entity.features.inc 6.7 KB

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