flag.features.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. * Features integration for Flag module.
  5. */
  6. /**
  7. * Implements hook_features_export().
  8. */
  9. function flag_features_export($data, &$export, $module_name = '') {
  10. $pipe = array();
  11. // Add flag module as a dependency.
  12. $export['dependencies']['flag'] = 'flag';
  13. // Ensure the modules that provide the flag are included as dependencies.
  14. $modules = flag_features_providing_module();
  15. foreach ($data as $key => $flag) {
  16. $module = '';
  17. if ($flag = flag_load($flag, TRUE)) {
  18. // Try to get the module that provides the entity this flag is on.
  19. // First pass: check whether there's a module that implements
  20. // hook_flag_type_info() for this entity.
  21. if (array_key_exists($flag->entity_type, $modules)) {
  22. $module = $modules[$flag->entity_type];
  23. }
  24. else {
  25. // Second pass: check whether this entity is defined using Entity API
  26. // and therefore has an extra 'module' property in its information.
  27. if ($entity_info = entity_get_info($flag->entity_type)) {
  28. if (isset($entity_info['module'])) {
  29. $module = $entity_info['module'];
  30. }
  31. }
  32. }
  33. if (!empty($module)) {
  34. $export['dependencies'][$module] = $module;
  35. }
  36. $export['features']['flag'][$flag->name] = $flag->name;
  37. }
  38. }
  39. return $pipe;
  40. }
  41. /**
  42. * Implements hook_features_export_options().
  43. */
  44. function flag_features_export_options() {
  45. $options = array();
  46. // Get all flags, including disabled defaults.
  47. $flags = flag_get_flags() + flag_get_default_flags(TRUE);
  48. foreach ($flags as $name => $flag) {
  49. $options[$name] = drupal_ucfirst(check_plain($flag->entity_type)) . ': ' . check_plain($flag->title);
  50. }
  51. return $options;
  52. }
  53. /**
  54. * Implements hook_features_export_render().
  55. */
  56. function flag_features_export_render($module, $data) {
  57. module_load_include('inc', 'flag', '/includes/flag.export');
  58. $code = flag_export_flags($data, $module, ' ');
  59. return array('flag_default_flags' => $code);
  60. }
  61. /**
  62. * Implements hook_features_revert().
  63. *
  64. * @param string $module
  65. * The name of module for which to revert content.
  66. */
  67. function flag_features_revert($module = NULL) {
  68. // Get default flags from features.
  69. if (module_hook($module, 'flag_default_flags')) {
  70. module_load_include('inc', 'flag', '/includes/flag.admin');
  71. $default_flags = module_invoke($module, 'flag_default_flags');
  72. // Build up values for the cache clear.
  73. $entity_types = array();
  74. // Revert flags that are defined in code.
  75. foreach ($default_flags as $flag_name => $flag_info) {
  76. if (is_numeric($flag_name)) {
  77. // Backward compatibility.
  78. $flag_name = $flag_info['name'];
  79. }
  80. $flag = flag_load($flag_name, TRUE);
  81. if ($flag && $flag->revert() === FALSE) {
  82. drupal_set_message(t('Could not revert flag %flag-name to the state described in your code: Your flag was created by a different version of the Flag module than is now being used.', array('%flag-name' => $flag->name)), 'error');
  83. }
  84. $entity_types[] = $flag->entity_type;
  85. }
  86. _flag_clear_cache($entity_types);
  87. }
  88. }
  89. /**
  90. * Helper function; Retrieve the providing modules defining the flags.
  91. */
  92. function flag_features_providing_module() {
  93. $modules = array();
  94. $hook = 'flag_type_info';
  95. foreach (module_implements($hook) as $module) {
  96. foreach (module_invoke($module, $hook) as $key => $value) {
  97. $modules[$key] = isset($value['module']) ? $value['module'] : $module;
  98. }
  99. }
  100. // Any entity type without a flag providing module will be provided by the
  101. // flag module.
  102. foreach (entity_get_info() as $entity_type => $entity) {
  103. if (!isset($modules[$entity_type]) && empty($entity['configuration']) && $entity_type !== 'taxonomy_vocabulary') {
  104. $modules[$entity_type] = 'flag';
  105. }
  106. }
  107. return $modules;
  108. }