Projet

Général

Profil

Paste
Télécharger (7,04 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / uuid / uuid.features.inc @ a5572547

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