Projet

Général

Profil

Paste
Télécharger (12,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid.core.inc @ 27945136

1
<?php
2

    
3
/**
4
 * @file
5
 * Implementation of UUID hooks for all core modules.
6
 *
7
 * @todo
8
 *   Replace all these hook implementations with a generic solution that uses
9
 *   info from hook_entity_field_info() and hook_entity_property_info(). That
10
 *   info should contain info about how UUIDs are mapped.
11
 */
12

    
13
/**
14
 * @defgroup uuid_property Propery implementations
15
 * @{
16
 */
17

    
18
/**
19
 * Implements hook_entity_uuid_load().
20
 */
21
function node_entity_uuid_load(&$entities, $entity_type) {
22
  if ($entity_type == 'node') {
23
    entity_property_id_to_uuid($entities, 'user', array('uid', 'revision_uid'));
24
    entity_property_id_to_uuid($entities, 'node', 'tnid');
25
  }
26
}
27

    
28
/**
29
 * Implements hook_entity_uuid_presave().
30
 */
31
function node_entity_uuid_presave(&$entity, $entity_type) {
32
  if ($entity_type == 'node') {
33
    entity_property_uuid_to_id($entity, 'user', array('uid', 'revision_uid'));
34
    entity_property_uuid_to_id($entity, 'node', 'tnid');
35
  }
36
}
37

    
38
/**
39
 * Implements hook_entity_uuid_save().
40
 */
41
function node_entity_uuid_save(&$entity, $entity_type) {
42
  // When a node is translated, the source node's tnid is set to it's own nid. When deploying
43
  // the node for the first time the tnid can't be translated to an nid until after the node has been saved.
44
  // So if the entity's tnid is still a uuid at this point it needs to be translated to an nid.
45
  if ($entity_type == 'node' && uuid_is_valid($entity->tnid)) {
46
    entity_property_uuid_to_id($entity, 'node', 'tnid');
47
    db_update('node')
48
      ->fields(array('tnid' => $entity->tnid))
49
      ->condition('nid', $entity->nid)
50
      ->execute();
51
  }
52
}
53

    
54
/**
55
 * Implements hook_entity_uuid_load().
56
 */
57
function book_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
58
  if ($entity_type == 'node') {
59
    if (!empty($entity->book)) {
60
      $entity->book['bid'] = current(entity_get_uuid_by_id($entity_type, array($entity->book['bid'])));
61
    }
62
  }
63
}
64

    
65
/**
66
 * Implements hook_entity_uuid_presave().
67
 */
68
function book_entity_uuid_presave(&$entity, $entity_type) {
69
  if ($entity_type == 'node') {
70
    if (!empty($entity->book)) {
71
      $entity->book['bid'] = current(entity_get_id_by_uuid($entity_type, array($entity->book['bid'])));
72
      if (!$entity->book['bid']) {
73
        $entity->book['bid'] = 'new';
74
      }
75
    }
76
  }
77
}
78

    
79
/**
80
 * Implements hook_entity_uuid_presave().
81
 */
82
function user_entity_uuid_presave(&$entity, $entity_type) {
83
  if ($entity_type == 'user') {
84
    if (!empty($entity->picture)) {
85
      $uuids = entity_get_id_by_uuid('file', array($entity->picture['uuid']));
86
      $fid = current($uuids);
87
      if (!$entity->is_new) {
88
        $entity->picture = file_load($fid);
89
      }
90
      else {
91
        $entity->picture = $fid;
92
      }
93
    }
94
  }
95
}
96

    
97
/**
98
 * Implements hook_entity_uuid_load().
99
 */
100
function comment_entity_uuid_load(&$entities, $entity_type) {
101
  switch ($entity_type) {
102
    case 'node':
103
      entity_property_id_to_uuid($entities, 'user', 'last_comment_uid');
104
      break;
105

    
106
    case 'comment':
107
      entity_property_id_to_uuid($entities, 'user', array('uid', 'u_uid'));
108
      entity_property_id_to_uuid($entities, 'node', 'nid');
109
      break;
110
  }
111
}
112

    
113
/**
114
 * Implements hook_entity_uuid_presave().
115
 */
116
function comment_entity_uuid_presave(&$entity, $entity_type) {
117
  switch ($entity_type) {
118
    case 'node':
119
      entity_property_uuid_to_id($entity, 'user', 'last_comment_uid');
120
      break;
121

    
122
    case 'comment':
123
      entity_property_uuid_to_id($entity, 'user', array('uid', 'u_uid'));
124
      entity_property_uuid_to_id($entity, 'node', 'nid');
125
      break;
126
  }
127
}
128

    
129
/**
130
 * Implements hook_entity_uuid_load().
131
 */
132
function file_entity_uuid_load(&$entities, $entity_type) {
133
  if ($entity_type == 'file') {
134
    entity_property_id_to_uuid($entities, 'user', 'uid');
135
  }
136
}
137

    
138
/**
139
 * Implements hook_entity_uuid_presave().
140
 */
141
function file_entity_uuid_presave(&$entity, $entity_type) {
142
  if ($entity_type == 'file') {
143
    entity_property_uuid_to_id($entity, 'user', 'uid');
144

    
145
    // Write the new file to the local filesystem.
146
    if (isset($entity->file_contents)) {
147
      // Don't try to write it if it uses a stream wrapper that isn't writeable
148
      // (for example, if it is a remotely-hosted video).
149
      $scheme = file_uri_scheme($entity->uri);
150
      $wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
151
      if (empty($wrappers[$scheme])) {
152
        return;
153
      }
154

    
155
      // Check for an existing file with the same URI.
156
      $existing_files = file_load_multiple(array(), array('uri' => $entity->uri));
157
      $existing = (object) array('uri' => NULL, 'uuid' => NULL);
158
      if (count($existing_files)) {
159
        $existing = reset($existing_files);
160
      }
161

    
162
      // If this is a new file and there is an existing file with the same URI,
163
      // but a different uuid then rename this file.
164
      if ($entity->is_new && $entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
165
        $uri = $entity->uri;
166
        $replace = FILE_EXISTS_RENAME;
167
      }
168
      // If this has an id, meaning UUID has already matched the uuid to an
169
      // existing file, but it has a URI that matches a file with a different
170
      // uuid, then load the file with the matching uuid and use the URI from
171
      // that file. The existing file with the matching uuid is most likely a
172
      // file that was previously renamed, e.g. as in the condition above, to
173
      // avoid conflict. The uuid matches because they are the same file, but
174
      // the URI does not because an incrementing number was added as part of
175
      // the renaming.
176
      elseif ($entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
177
        $file = file_load($entity->fid);
178
        $uri = $file->uri;
179
        $replace = FILE_EXISTS_REPLACE;
180
      }
181
      // Otherwise create a new file or replace the existing file contents.
182
      else {
183
        $uri = $entity->uri;
184
        $replace = FILE_EXISTS_REPLACE;
185
      }
186

    
187
      $directory = drupal_dirname($uri);
188
      file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
189
      $entity->uri = file_unmanaged_save_data(base64_decode($entity->file_contents), $uri, $replace);
190
    }
191
  }
192
}
193

    
194
/**
195
 * Implements hook_entity_uuid_load().
196
 */
197
function taxonomy_entity_uuid_load(&$entities, $entity_type) {
198
  if ($entity_type == 'taxonomy_term') {
199
    foreach ($entities as &$entity) {
200
      if (isset($entity->parent)) {
201
        if (!is_array($entity->parent)) {
202
          $entity->parent = array($entity->parent);
203
        }
204
        $uuids = entity_get_uuid_by_id('taxonomy_term', $entity->parent);
205
        $entity->parent = array_values($uuids);
206
      }
207
      unset($entity->vid);
208
    }
209
  }
210
}
211

    
212
/**
213
 * Implements hook_entity_uuid_presave().
214
 */
215
function taxonomy_entity_uuid_presave(&$entity, $entity_type) {
216
  if ($entity_type == 'taxonomy_term') {
217
    if (isset($entity->parent)) {
218
      if (!is_array($entity->parent)) {
219
        $entity->parent = array($entity->parent);
220
      }
221
      $ids = entity_get_id_by_uuid('taxonomy_term', $entity->parent);
222
      $entity->parent = array_values($ids);
223
    }
224
    $vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name);
225
    $entity->vid = $vocabulary->vid;
226
  }
227
}
228

    
229
/**
230
 * Implements hook_entity_uuid_load().
231
 */
232
function field_entity_uuid_load(&$entities, $entity_type) {
233
  foreach ($entities as $i => $entity) {
234
    list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
235
    $instances = field_info_instances($entity_type, $bundle_name);
236

    
237
    foreach ($instances as $field_name => $instance) {
238
      $field = field_info_field($field_name);
239
      if (!empty($field) && isset($entity->{$field_name})) {
240
        foreach ($entity->{$field_name} as $langcode => &$items) {
241
          // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
242
          // that is not passing by reference.
243
          $function = $field['module'] . '_field_uuid_load';
244
          if (function_exists($function)) {
245
            $function($entity_type, $entity, $field, $instance, $langcode, $items);
246
          }
247
        }
248
      }
249
    }
250
  }
251
}
252

    
253
/**
254
 * Implements hook_entity_uuid_presave().
255
 */
256
function field_entity_uuid_presave(&$entity, $entity_type) {
257
  list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
258
  $instances = field_info_instances($entity_type, $bundle_name);
259

    
260
  foreach ($instances as $field_name => $instance) {
261
    $field = field_info_field($field_name);
262
    if (!empty($field) && isset($entity->{$field_name})) {
263
      foreach ($entity->{$field_name} as $langcode => &$items) {
264
        // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
265
        // that is not passing by reference.
266
        $function = $field['module'] . '_field_uuid_presave';
267
        if (function_exists($function)) {
268
          $function($entity_type, $entity, $field, $instance, $langcode, $items);
269
        }
270
      }
271
    }
272
  }
273
}
274

    
275
/**
276
 * @} End of "Property implementations"
277
 */
278

    
279
/**
280
 * @defgroup uuid_field Field implementations
281
 * @{
282
 */
283

    
284
/**
285
 * Implements hook_field_uuid_load().
286
 */
287
function taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
288
  entity_property_id_to_uuid($items, 'taxonomy_term', 'tid');
289
}
290

    
291
/**
292
 * Implements hook_field_uuid_presave().
293
 */
294
function taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
295
  entity_property_uuid_to_id($items, 'taxonomy_term', 'tid');
296
}
297

    
298
/**
299
 * Implements hook_field_uuid_load().
300
 */
301
function file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
302
  entity_property_id_to_uuid($items, 'file', 'fid');
303
  entity_property_id_to_uuid($items, 'user', 'uid');
304
}
305

    
306
/**
307
 * Implements hook_field_uuid_presave().
308
 */
309
function file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
310
  entity_property_uuid_to_id($items, 'file', 'fid');
311
  entity_property_uuid_to_id($items, 'user', 'uid');
312
}
313

    
314
/**
315
 * Implements hook_field_uuid_load().
316
 */
317
function image_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
318
  file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
319
}
320

    
321
/**
322
 * Implements hook_field_uuid_presave().
323
 */
324
function image_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
325
  file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
326
}
327

    
328
/**
329
 * Implements hook_field_uuid_load().
330
 * Kept here because it is in D8 core.
331
 */
332
function entityreference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
333
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
334
  // can't handle a single $item.
335
  entity_property_id_to_uuid($items, $field['settings']['target_type'], 'target_id');
336
}
337

    
338
/**
339
 * Implements hook_field_uuid_presave().
340
 * Kept here because it is in D8 core.
341
 */
342
function entityreference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
343
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
344
  // can't handle a single $item.
345
  entity_property_uuid_to_id($items, $field['settings']['target_type'], 'target_id');
346
}
347

    
348
/**
349
 * @} End of "Field implementations"
350
 */
351

    
352
/**
353
 * @defgroup uuid_export Export alterations
354
 * @{
355
 */
356

    
357
/**
358
 * Implements hook_uuid_entities_features_export_entity_alter().
359
 */
360
function node_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
361
  if ($entity_type == 'node') {
362
    foreach (array('data', 'name', 'picture', 'revision_uid', 'last_comment_timestamp') as $property) {
363
      if (property_exists($entity, $property)) {
364
        unset($entity->{$property});
365
      }
366
    }
367
  }
368
}
369

    
370
/**
371
 * Implementations hook_uuid_entities_features_export_entity_alter().
372
 */
373
function user_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
374
  if ($entity_type == 'user') {
375
    foreach (array('data', 'access', 'login') as $property) {
376
      if (property_exists($entity, $property)) {
377
        unset($entity->{$property});
378
      }
379
    }
380
  }
381
}
382

    
383
/**
384
 * Implements hook_uuid_entities_features_export_alter().
385
 */
386
function file_uuid_entities_features_export_field_alter($entity_type, $entity, $field, $instance, $langcode, &$items) {
387
  foreach ($items as &$item) {
388
    if (isset($item['timestamp'])) {
389
      unset($item['timestamp']);
390
    }
391
  }
392
}
393

    
394
/**
395
 * @} End of "Export alterations"
396
 */