entity.features.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Provides Features integration for entity types using the CRUD API.
  5. */
  6. /**
  7. * Returns the configured entity features controller.
  8. *
  9. * @param string $type
  10. * The entity type to get the controller for.
  11. *
  12. * @return EntityDefaultFeaturesController
  13. * The configured entity features controller.
  14. */
  15. function entity_features_get_controller($type) {
  16. $static = &drupal_static(__FUNCTION__);
  17. if (!isset($static[$type])) {
  18. $info = entity_get_info($type);
  19. $info += array('features controller class' => 'EntityDefaultFeaturesController');
  20. $static[$type] = $info['features controller class'] ? new $info['features controller class']($type) : FALSE;
  21. }
  22. return $static[$type];
  23. }
  24. /**
  25. * Default controller handling features integration.
  26. */
  27. class EntityDefaultFeaturesController {
  28. protected $type, $info;
  29. protected $statusKey;
  30. protected $moduleKey;
  31. protected $bundleKey;
  32. public function __construct($type) {
  33. $this->type = $type;
  34. $this->info = entity_get_info($type);
  35. $this->info['entity keys'] += array('module' => 'module', 'status' => 'status');
  36. $this->statusKey = $this->info['entity keys']['status'];
  37. $this->moduleKey = $this->info['entity keys']['module'];
  38. if (!empty($this->info['bundle of'])) {
  39. $entity_info = entity_get_info($this->info['bundle of']);
  40. $this->bundleKey = $entity_info['bundle keys']['bundle'];
  41. }
  42. }
  43. /**
  44. * Defines the result for hook_features_api().
  45. */
  46. public function api() {
  47. return array(
  48. // The entity type has to be the features component name.
  49. $this->type => array(
  50. 'name' => $this->info['label'],
  51. 'feature_source' => TRUE,
  52. 'default_hook' => isset($this->info['export']['default hook']) ? $this->info['export']['default hook'] : 'default_' . $this->type,
  53. // Use the provided component callbacks making use of the controller.
  54. 'base' => 'entity',
  55. 'file' => drupal_get_path('module', 'entity') . '/entity.features.inc',
  56. ),
  57. );
  58. }
  59. /**
  60. * Generates the result for hook_features_export_options().
  61. */
  62. public function export_options() {
  63. $options = array();
  64. foreach (entity_load_multiple_by_name($this->type, FALSE) as $name => $entity) {
  65. $options[$name] = entity_label($this->type, $entity);
  66. }
  67. return $options;
  68. }
  69. /**
  70. * Generates the result for hook_features_export().
  71. */
  72. public function export($data, &$export, $module_name = '') {
  73. $pipe = array();
  74. foreach (entity_load_multiple_by_name($this->type, $data) as $name => $entity) {
  75. // If this entity is provided by a different module, add it as dependency.
  76. if (($entity->{$this->statusKey} & ENTITY_IN_CODE) && $entity->{$this->moduleKey} != $module_name) {
  77. $module = $entity->{$this->moduleKey};
  78. $export['dependencies'][$module] = $module;
  79. }
  80. // Otherwise export the entity.
  81. else {
  82. $export['features'][$this->type][$name] = $name;
  83. // If this is a bundle of a fieldable entity add its fields to the pipe.
  84. if (!empty($this->info['bundle of'])) {
  85. $fields = field_info_instances($this->info['bundle of'], $entity->{$this->bundleKey});
  86. foreach ($fields as $name => $field) {
  87. $pipe['field'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}";
  88. $pipe['field_instance'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}";
  89. }
  90. }
  91. }
  92. }
  93. // Add the module providing the entity type as dependency.
  94. if ($data && !empty($this->info['module'])) {
  95. $export['dependencies'][$this->info['module']] = $this->info['module'];
  96. // In case entity is not already an indirect dependency, add it.
  97. // We can do so without causing redundant dependencies because,
  98. // if entity is an indirect dependency, Features will filter it out.
  99. $export['dependencies']['entity'] = 'entity';
  100. }
  101. return $pipe;
  102. }
  103. /**
  104. * Generates the result for hook_features_export_render().
  105. */
  106. public function export_render($module, $data, $export = NULL) {
  107. $output = array();
  108. $output[] = ' $items = array();';
  109. foreach (entity_load_multiple_by_name($this->type, $data) as $name => $entity) {
  110. $export = " \$items['$name'] = entity_import('{$this->type}', '";
  111. // Make sure to escape the characters \ and '.
  112. $export .= addcslashes(entity_export($this->type, $entity, ' '), '\\\'');
  113. $export .= "');";
  114. $output[] = $export;
  115. }
  116. $output[] = ' return $items;';
  117. $output = implode("\n", $output);
  118. $hook = isset($this->info['export']['default hook']) ? $this->info['export']['default hook'] : 'default_' . $this->type;
  119. return array($hook => $output);
  120. }
  121. /**
  122. * Generates the result for hook_features_revert().
  123. */
  124. public function revert($module = NULL) {
  125. if ($defaults = features_get_default($this->type, $module)) {
  126. entity_delete_multiple($this->type, array_keys($defaults));
  127. }
  128. }
  129. }
  130. /**
  131. * Implements hook_features_api().
  132. */
  133. function entity_features_api() {
  134. $items = array();
  135. foreach (entity_crud_get_info() as $type => $info) {
  136. if (!empty($info['exportable']) && $controller = entity_features_get_controller($type)) {
  137. $items += $controller->api();
  138. }
  139. }
  140. return $items;
  141. }
  142. /**
  143. * Implements hook_features_export_options().
  144. *
  145. * Features component callback.
  146. */
  147. function entity_features_export_options($a1, $a2 = NULL) {
  148. // Due to a change in the Features API the first parameter might be a feature
  149. // object or an entity type, depending on the Features version. This check is
  150. // for backwards compatibility.
  151. $entity_type = is_string($a1) ? $a1 : $a2;
  152. return entity_features_get_controller($entity_type)->export_options();
  153. }
  154. /**
  155. * Implements hook_features_export().
  156. *
  157. * Features component callback.
  158. */
  159. function entity_features_export($data, &$export, $module_name, $entity_type) {
  160. return entity_features_get_controller($entity_type)->export($data, $export, $module_name);
  161. }
  162. /**
  163. * Implements hook_features_export_render().
  164. *
  165. * Features component callback.
  166. */
  167. function entity_features_export_render($module, $data, $export, $entity_type) {
  168. return entity_features_get_controller($entity_type)->export_render($module, $data, $export);
  169. }
  170. /**
  171. * Implements hook_features_revert().
  172. *
  173. * Features component callback.
  174. */
  175. function entity_features_revert($module, $entity_type) {
  176. return entity_features_get_controller($entity_type)->revert($module);
  177. }
  178. /**
  179. * Implements hook_features_post_restore().
  180. *
  181. * Rebuild all defaults when a features rebuild is triggered - even the ones not
  182. * handled by features itself.
  183. */
  184. function entity_features_post_restore($op, $items = array()) {
  185. if ($op == 'rebuild') {
  186. // Use features rebuild to rebuild the features independent exports too.
  187. entity_defaults_rebuild();
  188. }
  189. }