materio_flag.install 2.4 KB

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