Projet

Général

Profil

Révision e7101f36

Ajouté par Julien Enselme il y a environ 9 ans

Update uuid 1.0-alpha5 1.0-alpha6

Voir les différences:

drupal7/sites/all/modules/uuid/uuid.entity.inc
76 76
 */
77 77

  
78 78
/**
79
 * Implements of hook_entity_info_alter().
79
 * Implements hook_entity_info_alter().
80 80
 *
81
 * @see uuid_core_entity_info().
81
 * @see uuid_core_entity_info()
82 82
 */
83 83
function uuid_entity_info_alter(&$info) {
84 84
  foreach (uuid_get_core_entity_info() as $entity_type => $core_info) {
......
91 91
}
92 92

  
93 93
/**
94
 * Implements of hook_entity_property_info_alter().
94
 * Implements hook_entity_property_info_alter().
95 95
 *
96 96
 * This adds the UUID as an entity property for all UUID-enabled entities
97 97
 * which automatically gives us token and Rules integration.
98 98
 */
99 99
function uuid_entity_property_info_alter(&$info) {
100 100
  foreach (entity_get_info() as $entity_type => $entity_info) {
101
    if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && !empty($entity_info['entity keys']['uuid'])) {
101
    if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE 
102
      && !empty($entity_info['entity keys']['uuid'])
103
      && empty($info[$entity_type]['properties'][$entity_info['entity keys']['uuid']])) {
102 104
      $info[$entity_type]['properties'][$entity_info['entity keys']['uuid']] = array(
103 105
        'label' => t('UUID'),
104 106
        'type' => 'text',
105 107
        'description' => t('The universally unique ID.'),
106 108
        'schema field' => $entity_info['entity keys']['uuid'],
107 109
      );
108
      if (!empty($entity_info['entity keys']['revision uuid'])) {
110
      if (!empty($entity_info['entity keys']['revision uuid']) 
111
        && empty($info[$entity_type]['properties'][$entity_info['entity keys']['revision uuid']])) {
109 112
        $info[$entity_type]['properties'][$entity_info['entity keys']['revision uuid']] = array(
110 113
          'label' => t('Revision UUID'),
111 114
          'type' => 'text',
......
118 121
}
119 122

  
120 123
/**
121
 * Implements of hook_entity_presave().
124
 * Implements hook_entity_presave().
122 125
 *
123 126
 * This is where all UUID-enabled entities get their UUIDs.
124 127
 */
......
131 134
    }
132 135
    if (!empty($info['entity keys']['revision uuid'])) {
133 136
      $vuuid_key = $info['entity keys']['revision uuid'];
134
      if ((isset($entity->revision) && $entity->revision == TRUE) || empty($entity->{$vuuid_key})) {
137
      // If this entity comes from a remote environment and have a revision UUID
138
      // that exists locally we should not create a new revision. Because
139
      // otherwise revisions won't be tracked universally.
140
      // TODO: Move code dependent on the uuid_services module into it's own
141
      // implementation of hook_entity_presave().
142
      if (!empty($entity->uuid_services) && isset($entity->{$vuuid_key})) {
143
        $vuuid_exists = (bool) entity_get_id_by_uuid($entity_type, array($entity->{$vuuid_key}), TRUE);
144
        if ($vuuid_exists) {
145
          $entity->revision = FALSE;
146
        }
147
      }
148

  
149
      if ((isset($entity->revision) && $entity->revision == TRUE && empty($entity->uuid_services)) || empty($entity->{$vuuid_key})) {
135 150
        $entity->{$vuuid_key} = uuid_generate();
136 151
      }
137 152
    }
......
151 166
/**
152 167
 * Load entities by their UUID, that only should containing UUID references.
153 168
 *
169
 * Optionally load revisions by their VUUID by passing it into $conditions.
170
 * Ex. $conditions['vuuid'][$vuuid]
171
 *
154 172
 * This function is mostly useful if you want to load an entity from the local
155 173
 * database that only should contain UUID references.
156 174
 *
157 175
 * @see entity_load()
158 176
 */
159 177
function entity_uuid_load($entity_type, $uuids = array(), $conditions = array(), $reset = FALSE) {
178
  // Allow Revision UUID to be passed in $conditions and translate.
179
  $entity_info[$entity_type] = entity_get_info($entity_type);
180
  $revision_key = $entity_info[$entity_type]['entity keys']['revision'];
181
  if (isset($entity_info[$entity_type]['entity keys']['revision uuid'])) {
182
    $revision_uuid_key = $entity_info[$entity_type]['entity keys']['revision uuid'];
183
  }
184
  if (isset($revision_uuid_key) && isset($conditions[$revision_uuid_key])) {
185
    $revision_id = entity_get_id_by_uuid($entity_type, array($conditions[$revision_uuid_key]), TRUE);
186
    $conditions[$revision_key] = $revision_id[$conditions[$revision_uuid_key]];
187
    unset($conditions[$revision_uuid_key]);
188
  }
160 189
  $ids = entity_get_id_by_uuid($entity_type, $uuids);
161 190
  $results = entity_load($entity_type, $ids, $conditions, $reset);
162 191
  $entities = array();
......
209 238
    throw new UuidEntityException(t('Calling %function requires the Entity API module (!link).', array('%function' => __FUNCTION__, '!link' => 'http://drupal.org/project/entity')));
210 239
  }
211 240

  
241
  $info = entity_get_info($entity_type);
242
  $uuid_key = $info['entity keys']['uuid'];
243
  if (empty($entity->{$uuid_key}) || !uuid_is_valid($entity->{$uuid_key})) {
244
    watchdog('Entity UUID', 'Attempted to save an entity with an invalid UUID', array(), WATCHDOG_ERROR);
245
    return FALSE;
246
  }
247

  
248
  // Falling back on the variable node_options_[type] is not something an API
249
  // function should take care of. With normal (non UUID) nodes this is dealt
250
  // with in the form submit handler, i.e. not in node_save().
251
  // But since using entity_uuid_save() usually means you're trying to manage
252
  // entities remotely we do respect this variable here to make it work as the
253
  // node form, but only if we explicitly haven't set $node->revision already.
254
  if ($entity_type == 'node' && !isset($entity->revision) && in_array('revision', variable_get('node_options_' . $entity->type, array()))) {
255
    $entity->revision = 1;
256
  }
257

  
212 258
  entity_make_entity_local($entity_type, $entity);
213 259

  
214 260
  // Save the entity.
......
259 305
      $vid = NULL;
260 306
      // Fetch the local revision ID by its UUID.
261 307
      if (isset($entity->{$vuuid_key})) {
308
        // It's important to note that the revision UUID might be set here but
309
        // there might not exist a correspondant local revision ID in which case
310
        // we should unset the assigned revision ID to not confuse anyone with
311
        // revision IDs that might come from other environments.
262 312
        $vids = entity_get_id_by_uuid($entity_type, array($entity->{$vuuid_key}), TRUE);
263 313
        $vid = reset($vids);
264 314
      }
......
268 318
      elseif (!empty($vid)) {
269 319
        $entity->{$vid_key} = $vid;
270 320
      }
271
      // Nodes need this when trying to save an existing node without a vid.
321
      // If the revision ID was unset before this (or just missing for some
322
      // reason) we fetch the current revision ID to build a better
323
      // representation of the node object we're working with.
272 324
      if ($entity_type == 'node' && !isset($entity->vid) && !$entity->is_new) {
273
        $entity->revision = 0;
274 325
        $entity->vid = db_select('node', 'n')
275 326
          ->condition('n.nid', $entity->nid)
276 327
          ->fields('n', array('vid'))
......
312 363
    // Fetch the local ID by its UUID.
313 364
    $ids = entity_get_id_by_uuid($entity_type, array($uuid));
314 365
    $id = reset($ids);
366
    $entity = entity_load($entity_type, array($id));
315 367

  
316 368
    // Let other modules transform UUID references to local ID references.
317 369
    $hook = 'entity_uuid_delete';
......
322 374
      }
323 375
    }
324 376

  
377
    if (empty($entity)) {
378
      return FALSE;
379
    }
325 380
    // Delete the entity.
326 381
    return entity_delete($entity_type, $id);
327 382
  }

Formats disponibles : Unified diff