entity.features.inc 5.9 KB

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