flag_bookmark.install 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @file
  4. * The Flag Bookmark module install hooks.
  5. */
  6. /**
  7. * Implements hook_enable().
  8. *
  9. * We create the demonstration flag on enable, so hook implementations in flag
  10. * module will fire correctly, as the APIs are not available on install.
  11. */
  12. function flag_bookmark_enable() {
  13. // Load the flag API in case we want to use it when enabling.
  14. include_once drupal_get_path('module', 'flag') . '/flag.module';
  15. if (!flag_get_flags()) {
  16. // Install a demonstration flag only if no flag exists. This is to prevent
  17. // a case where a disables and enables the module, and the demonstration
  18. // flag is overwritten or re-created.
  19. $flag = flag_flag::factory_by_entity_type('node');
  20. $configuration = array(
  21. 'name' => 'bookmarks',
  22. 'global' => 0,
  23. 'show_in_links' => array(
  24. 'full' => 1,
  25. 'teaser' => 1,
  26. ),
  27. 'show_on_form' => 1,
  28. // The following UI labels aren't wrapped in t() because they are written
  29. // to the DB in English. They are passed to t() later, thus allowing for
  30. // multilingual sites.
  31. 'title' => 'Bookmarks',
  32. 'flag_short' => 'Bookmark this',
  33. 'flag_long' => 'Add this post to your bookmarks',
  34. 'flag_message' => 'This post has been added to your bookmarks',
  35. 'unflag_short' => 'Unbookmark this',
  36. 'unflag_long' => 'Remove this post from your bookmarks',
  37. 'unflag_message' => 'This post has been removed from your bookmarks',
  38. 'types' => _flag_bookmark_install_get_suggested_node_types(),
  39. );
  40. $flag->form_input($configuration);
  41. $flag->save();
  42. // Clear the flag cache so the new permission is seen by core.
  43. drupal_static_reset('flag_get_flags');
  44. // Grant permissions.
  45. $permissions = array('flag bookmarks', 'unflag bookmarks');
  46. user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions);
  47. }
  48. }
  49. /**
  50. * Returns some node types to which the demonstration 'bookmarks' flag will
  51. * apply.
  52. */
  53. function _flag_bookmark_install_get_suggested_node_types() {
  54. $preferred = array('article', 'story', 'forum', 'blog');
  55. $existing = array_intersect($preferred, array_keys(node_type_get_types()));
  56. if (!$existing) {
  57. // As a last resort, take the first preference.
  58. return array($preferred[0]);
  59. }
  60. return $existing;
  61. }