uuid.features.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(
  83. 'created',
  84. 'updated',
  85. 'changed',
  86. 'revision_timestamp',
  87. 'timestamp',
  88. 'stamp',
  89. 'current',
  90. );
  91. foreach ($keys as $key) {
  92. if (isset($entity->{$key})) {
  93. unset($entity->{$key});
  94. }
  95. }
  96. // Let other modules alter exported entities.
  97. drupal_alter('uuid_entities_features_export_entity', $entity, $entity_type);
  98. // Field handling.
  99. list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
  100. $instances = field_info_instances($entity_type, $bundle_name);
  101. foreach ($instances as $field_name => $instance) {
  102. $field = field_info_field($field_name);
  103. if (!empty($entity->{$field_name})) {
  104. foreach ($entity->{$field_name} as $langcode => &$items) {
  105. // Let other modules alter fields on exported entities.
  106. // We are not using drupal_alter() here, because of it's complexity
  107. // dealing with over two alterable arguments.
  108. $hook = 'uuid_entities_features_export_field_alter';
  109. foreach (module_implements($hook) as $module_name) {
  110. $function = $module_name . '_' . $hook;
  111. $function($entity_type, $entity, $field, $instance, $langcode, $items);
  112. }
  113. foreach ($items as &$item) {
  114. // We don't need to export these common field keys.
  115. foreach (array('safe_value', 'safe_summary') as $key) {
  116. if (isset($item[$key])) {
  117. unset($item[$key]);
  118. }
  119. }
  120. uuid_entities_features_clean($item);
  121. }
  122. }
  123. }
  124. }
  125. uuid_entities_features_clean($entity);
  126. /*
  127. * Convert entities to array to avoid having them in JSON, returned
  128. * from standard implementation of $entity->export().
  129. */
  130. if (is_object($entity) && method_exists($entity, 'export')) {
  131. $entity = get_object_vars($entity);
  132. }
  133. $code[] = ' $entities[\'' . check_plain($component) . '\'][] = (object) ' . features_var_export($entity, ' ') . ';';
  134. }
  135. }
  136. $code[] = '';
  137. $code[] = ' return $entities;';
  138. return array('uuid_default_entities' => implode("\n", $code));
  139. }
  140. /**
  141. * Implements [component]_features_rebuild().
  142. */
  143. function uuid_entities_features_rebuild($module_name) {
  144. uuid_entities_rebuild($module_name, 'rebuild');
  145. }
  146. /**
  147. * Implements [component]_features_revert().
  148. */
  149. function uuid_entities_features_revert($module_name) {
  150. uuid_entities_rebuild($module_name, 'revert');
  151. }
  152. /**
  153. * Helper function to rebuild entities from a plan.
  154. */
  155. function uuid_entities_rebuild($module_name = '', $op = 'rebuild') {
  156. features_include_defaults(array('uuid_entities'));
  157. $entities = module_invoke($module_name, 'uuid_default_entities');
  158. if (!empty($entities)) {
  159. foreach ($entities as $plan_name => $entities) {
  160. // Let other modules do things before default entities are created.
  161. module_invoke_all("uuid_entities_pre_$op", $plan_name);
  162. drupal_alter("uuid_entities_pre_$op", $entities, $plan_name);
  163. foreach ($entities as $entity) {
  164. entity_uuid_save($entity->__metadata['type'], $entity);
  165. }
  166. // Let other modules do things after default entities are created.
  167. module_invoke_all("uuid_entities_post_$op", $plan_name);
  168. }
  169. }
  170. }
  171. /**
  172. * Helper function to sort properties of an object.
  173. *
  174. * This will maintain better consistency. Keys might get shifted order or type
  175. * due to alterations sometimes.
  176. */
  177. function uuid_entities_features_clean(&$object) {
  178. $properties = array();
  179. foreach ($object as $key => $value) {
  180. $properties[$key] = $value;
  181. if (is_object($object)) {
  182. unset($object->{$key});
  183. }
  184. elseif (is_array($object)) {
  185. unset($object[$key]);
  186. }
  187. }
  188. ksort($properties);
  189. foreach ($properties as $key => $value) {
  190. // Make properties type consistent.
  191. if (is_string($value) || is_numeric($value)) {
  192. if (is_object($object)) {
  193. $object->{$key} = "$value";
  194. }
  195. elseif (is_array($object)) {
  196. $object[$key] = "$value";
  197. }
  198. }
  199. else {
  200. if (is_object($object)) {
  201. $object->{$key} = $value;
  202. }
  203. elseif (is_array($object)) {
  204. $object[$key] = $value;
  205. }
  206. }
  207. }
  208. }