uuid.features.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @file
  4. * Features support to export entities from any Deploy <em>fetch-only</em> plan.
  5. */
  6. /**
  7. * Implements [component]_features_export_options().
  8. *
  9. * Deploy plans are used as the initial source for an export. Deploy solves
  10. * complex dependency chains for us, so the entities actually can be used as is.
  11. */
  12. function uuid_entities_features_export_options() {
  13. $options = array();
  14. if (module_exists('deploy')) {
  15. $plans = deploy_plan_load_all(array('fetch_only' => 1));
  16. foreach ($plans as $plan) {
  17. $options[$plan->name] = $plan->title;
  18. }
  19. }
  20. return $options;
  21. }
  22. /**
  23. * Implements [component]_features_export().
  24. */
  25. function uuid_entities_features_export($components, &$export, $module_name) {
  26. foreach ($components as $plan_name) {
  27. $export['features']['uuid_entities'][$plan_name] = $plan_name;
  28. }
  29. }
  30. /**
  31. * Implements [component]_features_export_render().
  32. *
  33. * Components corresponds to the name of Deploy plans. However, once exported,
  34. * entities can be rendered directly from the default hook without Deploy or the
  35. * initial plan.
  36. */
  37. function uuid_entities_features_export_render($module_name, $components, $export = NULL) {
  38. $code = array();
  39. $code[] = ' $entities = array();';
  40. $code[] = '';
  41. foreach ($components as $component) {
  42. $entities = array();
  43. if (module_exists('deploy') && $plan = deploy_plan_load($component)) {
  44. $entities = $plan->getIterator();
  45. }
  46. else {
  47. features_include_defaults(array('uuid_entities'));
  48. $default_entities = module_invoke($module_name, 'uuid_default_entities');
  49. foreach ($default_entities[$component] as $entity) {
  50. $metadata = $entity->__metadata;
  51. $entity_type = $metadata['type'];
  52. $entity_info = entity_get_info($entity_type);
  53. $results = entity_uuid_load($entity_type, array($entity->{$entity_info['entity keys']['uuid']}), NULL, TRUE);
  54. if (!empty($results)) {
  55. $entity = reset($results);
  56. // Re-attach the metadata after loading the clean entity.
  57. $entity->__metadata = $metadata;
  58. $entities[] = $entity;
  59. }
  60. }
  61. }
  62. foreach ($entities as $entity) {
  63. $entity_type = $entity->__metadata['type'];
  64. $entity_info = entity_get_info($entity_type);
  65. // We need to remove entity id and revision keys for better consistency.
  66. $id_key = $entity_info['entity keys']['id'];
  67. if (isset($entity->{$id_key})) {
  68. unset($entity->{$id_key});
  69. }
  70. if (!empty($entity_info['entity keys']['revision'])) {
  71. $vid_key = $entity_info['entity keys']['revision'];
  72. if (isset($entity->{$vid_key})) {
  73. unset($entity->{$vid_key});
  74. }
  75. if (!empty($entity_info['entity keys']['revision uuid'])) {
  76. $vuuid_key = $entity_info['entity keys']['revision uuid'];
  77. unset($entity->{$vuuid_key});
  78. }
  79. }
  80. // We unset some common timestamp properties, since those will change and
  81. // constantly leave the feature overidden.
  82. $keys = array('created', 'updated', 'changed', 'revision_timestamp', 'timestamp', 'stamp', 'current');
  83. foreach ($keys as $key) {
  84. if (isset($entity->{$key})) {
  85. unset($entity->{$key});
  86. }
  87. }
  88. // Let other modules alter exported entities.
  89. drupal_alter('uuid_entities_features_export_entity', $entity, $entity_type);
  90. // Field handling.
  91. list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
  92. $instances = field_info_instances($entity_type, $bundle_name);
  93. foreach ($instances as $field_name => $instance) {
  94. $field = field_info_field($field_name);
  95. if (!empty($entity->{$field_name})) {
  96. foreach ($entity->{$field_name} as $langcode => &$items) {
  97. // Let other modules alter fields on exported entities.
  98. // We are not using drupal_alter() here, because of it's complexity
  99. // dealing with over two alterable arguments.
  100. $hook = 'uuid_entities_features_export_field_alter';
  101. foreach (module_implements($hook) as $module_name) {
  102. $function = $module_name . '_' . $hook;
  103. $function($entity_type, $entity, $field, $instance, $langcode, $items);
  104. }
  105. foreach ($items as &$item) {
  106. // We don't need to export these common field keys.
  107. foreach (array('safe_value', 'safe_summary') as $key) {
  108. if (isset($item[$key])) {
  109. unset($item[$key]);
  110. }
  111. }
  112. uuid_entities_features_clean($item);
  113. }
  114. }
  115. }
  116. }
  117. uuid_entities_features_clean($entity);
  118. // Convert entities to array to avoid having them in JSON, returned from standard implementation of $entity->export().
  119. if (is_object($entity) && method_exists($entity, 'export')) {
  120. $entity = get_object_vars($entity);
  121. }
  122. $code[] = ' $entities[\'' . check_plain($component) . '\'][] = (object) ' . features_var_export($entity, ' ') . ';';
  123. }
  124. }
  125. $code[] = '';
  126. $code[] = ' return $entities;';
  127. return array('uuid_default_entities' => implode("\n", $code));
  128. }
  129. /**
  130. * Implements [component]_features_export_rebuild().
  131. */
  132. function uuid_entities_features_rebuild($module_name) {
  133. uuid_entities_rebuild($module_name, 'rebuild');
  134. }
  135. /**
  136. * Implements [component]_features_export_revert().
  137. */
  138. function uuid_entities_features_revert($module_name) {
  139. uuid_entities_rebuild($module_name, 'revert');
  140. }
  141. /**
  142. * Helper function to rebuild entities from a plan.
  143. */
  144. function uuid_entities_rebuild($module_name = '', $op = 'rebuild') {
  145. features_include_defaults(array('uuid_entities'));
  146. $entities = module_invoke($module_name, 'uuid_default_entities');
  147. if (!empty($entities)) {
  148. foreach ($entities as $plan_name => $entities) {
  149. // Let other modules do things before default entities are created.
  150. module_invoke_all("uuid_entities_pre_$op", $plan_name);
  151. foreach ($entities as $entity) {
  152. entity_uuid_save($entity->__metadata['type'], $entity);
  153. }
  154. // Let other modules do things after default entities are created.
  155. module_invoke_all("uuid_entities_post_$op", $plan_name);
  156. }
  157. }
  158. }
  159. /**
  160. * Helper function to sort properties of an object.
  161. *
  162. * This will maintain better consistency. Keys might get shifted order or type
  163. * due to alterations sometimes.
  164. */
  165. function uuid_entities_features_clean(&$object) {
  166. $properties = array();
  167. foreach ($object as $key => $value) {
  168. $properties[$key] = $value;
  169. if (is_object($object)) {
  170. unset($object->{$key});
  171. }
  172. elseif (is_array($object)) {
  173. unset($object[$key]);
  174. }
  175. }
  176. ksort($properties);
  177. foreach ($properties as $key => $value) {
  178. // Make properties type consistent.
  179. if (is_string($value) || is_numeric($value)) {
  180. if (is_object($object)) {
  181. $object->{$key} = "$value";
  182. }
  183. elseif (is_array($object)) {
  184. $object[$key] = "$value";
  185. }
  186. }
  187. else {
  188. if (is_object($object)) {
  189. $object->{$key} = $value;
  190. }
  191. elseif (is_array($object)) {
  192. $object[$key] = $value;
  193. }
  194. }
  195. }
  196. }