Projet

Général

Profil

Paste
Télécharger (13,5 ko) Statistiques
| Branche: | Révision:

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

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_load().
40
 */
41
function book_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
42
  if ($entity_type == 'node') {
43
    if (!empty($entity->book)) {
44
      $entity->book['bid'] = current(entity_get_uuid_by_id($entity_type, array($entity->book['bid'])));
45
    }
46
  }
47
}
48

    
49
/**
50
 * Implements hook_entity_uuid_presave().
51
 */
52
function book_entity_uuid_presave(&$entity, $entity_type) {
53
  if ($entity_type == 'node') {
54
    if (!empty($entity->book)) {
55
      $entity->book['bid'] = current(entity_get_id_by_uuid($entity_type, array($entity->book['bid'])));
56
      if (!$entity->book['bid']) {
57
        $entity->book['bid'] = 'new';
58
      }
59
    }
60
  }
61
}
62

    
63
/**
64
 * Implements hook_entity_uuid_presave().
65
 */
66
function user_entity_uuid_presave(&$entity, $entity_type) {
67
  if ($entity_type == 'user') {
68
    if (!empty($entity->picture)) {
69
      $uuids = entity_get_id_by_uuid('file', array($entity->picture['uuid']));
70
      $fid = current($uuids);
71
      if (!$entity->is_new) {
72
        $entity->picture = file_load($fid);
73
      }
74
      else {
75
        $entity->picture = $fid;
76
      }
77
    }
78
  }
79
}
80

    
81
/**
82
 * Implements hook_entity_uuid_load().
83
 */
84
function comment_entity_uuid_load(&$entities, $entity_type) {
85
  switch ($entity_type) {
86
    case 'node':
87
      entity_property_id_to_uuid($entities, 'user', 'last_comment_uid');
88
      break;
89

    
90
    case 'comment':
91
      entity_property_id_to_uuid($entities, 'user', array('uid', 'u_uid'));
92
      entity_property_id_to_uuid($entities, 'node', 'nid');
93
      break;
94
  }
95
}
96

    
97
/**
98
 * Implements hook_entity_uuid_presave().
99
 */
100
function comment_entity_uuid_presave(&$entity, $entity_type) {
101
  switch ($entity_type) {
102
    case 'node':
103
      entity_property_uuid_to_id($entity, 'user', 'last_comment_uid');
104
      break;
105

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

    
113
/**
114
 * Implements hook_entity_uuid_load().
115
 */
116
function file_entity_uuid_load(&$entities, $entity_type) {
117
  if ($entity_type == 'file') {
118
    entity_property_id_to_uuid($entities, 'user', 'uid');
119
  }
120
}
121

    
122
/**
123
 * Implements hook_entity_uuid_presave().
124
 */
125
function file_entity_uuid_presave(&$entity, $entity_type) {
126
  if ($entity_type == 'file') {
127
    entity_property_uuid_to_id($entity, 'user', 'uid');
128

    
129
    // Write the new file to the local filesystem.
130
    if (isset($entity->file_contents)) {
131
      // Don't try to write it if it uses a stream wrapper that isn't writeable
132
      // (for example, if it is a remotely-hosted video).
133
      $scheme = file_uri_scheme($entity->uri);
134
      $wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
135
      if (empty($wrappers[$scheme])) {
136
        return;
137
      }
138

    
139
      // Check for an existing file with the same URI.
140
      $existing_files = file_load_multiple(array(), array('uri' => $entity->uri));
141
      $existing = (object) array('uri' => NULL, 'uuid' => NULL);
142
      if (count($existing_files)) {
143
        $existing = reset($existing_files);
144
      }
145

    
146
      // If this is a new file and there is an existing file with the same URI,
147
      // but a different uuid then rename this file.
148
      if ($entity->is_new && $entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
149
        $uri = $entity->uri;
150
        $replace = FILE_EXISTS_RENAME;
151
      }
152
      // If this has an id, meaning UUID has already matched the uuid to an
153
      // existing file, but it has a URI that matches a file with a different
154
      // uuid, then load the file with the matching uuid and use the URI from
155
      // that file. The existing file with the matching uuid is most likely a
156
      // file that was previously renamed, e.g. as in the condition above, to
157
      // avoid conflict. The uuid matches because they are the same file, but
158
      // the URI does not because an incrementing number was added as part of
159
      // the renaming.
160
      elseif ($entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
161
        $file = file_load($entity->fid);
162
        $uri = $file->uri;
163
        $replace = FILE_EXISTS_REPLACE;
164
      }
165
      // Otherwise create a new file or replace the existing file contents.
166
      else {
167
        $uri = $entity->uri;
168
        $replace = FILE_EXISTS_REPLACE;
169
      }
170

    
171
      $directory = drupal_dirname($uri);
172
      file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
173
      $entity->uri = file_unmanaged_save_data(base64_decode($entity->file_contents), $uri, $replace);
174
    }
175
  }
176
}
177

    
178
/**
179
 * Implements hook_entity_uuid_load().
180
 */
181
function taxonomy_entity_uuid_load(&$entities, $entity_type) {
182
  if ($entity_type == 'taxonomy_term') {
183
    foreach ($entities as &$entity) {
184
      if (isset($entity->parent)) {
185
        if (!is_array($entity->parent)) {
186
          $entity->parent = array($entity->parent);
187
        }
188
        $uuids = entity_get_uuid_by_id('taxonomy_term', $entity->parent);
189
        $entity->parent = array_values($uuids);
190
      }
191
      unset($entity->vid);
192
    }
193
  }
194
}
195

    
196
/**
197
 * Implements hook_entity_uuid_presave().
198
 */
199
function taxonomy_entity_uuid_presave(&$entity, $entity_type) {
200
  if ($entity_type == 'taxonomy_term') {
201
    if (isset($entity->parent)) {
202
      if (!is_array($entity->parent)) {
203
        $entity->parent = array($entity->parent);
204
      }
205
      $ids = entity_get_id_by_uuid('taxonomy_term', $entity->parent);
206
      $entity->parent = array_values($ids);
207
    }
208
    $vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name);
209
    $entity->vid = $vocabulary->vid;
210
  }
211
}
212

    
213
/**
214
 * Implements hook_entity_uuid_load().
215
 */
216
function field_entity_uuid_load(&$entities, $entity_type) {
217
  foreach ($entities as $i => $entity) {
218
    list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
219
    $instances = field_info_instances($entity_type, $bundle_name);
220

    
221
    foreach ($instances as $field_name => $instance) {
222
      $field = field_info_field($field_name);
223
      if (!empty($field) && isset($entity->{$field_name})) {
224
        foreach ($entity->{$field_name} as $langcode => &$items) {
225
          // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
226
          // that is not passing by reference.
227
          $function = $field['module'] . '_field_uuid_load';
228
          if (function_exists($function)) {
229
            $function($entity_type, $entity, $field, $instance, $langcode, $items);
230
          }
231
        }
232
      }
233
    }
234
  }
235
}
236

    
237
/**
238
 * Implements hook_entity_uuid_presave().
239
 */
240
function field_entity_uuid_presave(&$entity, $entity_type) {
241
  list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
242
  $instances = field_info_instances($entity_type, $bundle_name);
243

    
244
  foreach ($instances as $field_name => $instance) {
245
    $field = field_info_field($field_name);
246
    if (!empty($field) && isset($entity->{$field_name})) {
247
      foreach ($entity->{$field_name} as $langcode => &$items) {
248
        // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
249
        // that is not passing by reference.
250
        $function = $field['module'] . '_field_uuid_presave';
251
        if (function_exists($function)) {
252
          $function($entity_type, $entity, $field, $instance, $langcode, $items);
253
        }
254
      }
255
    }
256
  }
257
}
258

    
259
/**
260
 * @} End of "Property implementations"
261
 */
262

    
263
/**
264
 * @defgroup uuid_field Field implementations
265
 * @{
266
 */
267

    
268
/**
269
 * Implements hook_field_uuid_load().
270
 */
271
function taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
272
  entity_property_id_to_uuid($items, 'taxonomy_term', 'tid');
273
}
274

    
275
/**
276
 * Implements hook_field_uuid_presave().
277
 */
278
function taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
279
  entity_property_uuid_to_id($items, 'taxonomy_term', 'tid');
280
}
281

    
282
/**
283
 * Implements hook_field_uuid_load().
284
 */
285
function file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
286
  entity_property_id_to_uuid($items, 'file', 'fid');
287
  entity_property_id_to_uuid($items, 'user', 'uid');
288
}
289

    
290
/**
291
 * Implements hook_field_uuid_presave().
292
 */
293
function file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
294
  entity_property_uuid_to_id($items, 'file', 'fid');
295
  entity_property_uuid_to_id($items, 'user', 'uid');
296
}
297

    
298
/**
299
 * Implements hook_field_uuid_load().
300
 */
301
function image_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
302
  file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
303
}
304

    
305
/**
306
 * Implements hook_field_uuid_presave().
307
 */
308
function image_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
309
  file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
310
}
311

    
312
/**
313
 * Implements hook_field_uuid_load().
314
 */
315
function node_reference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
316
  entity_property_id_to_uuid($items, 'node', 'nid');
317
}
318

    
319
/**
320
 * Implements hook_field_uuid_presave().
321
 */
322
function node_reference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
323
  entity_property_uuid_to_id($items, 'node', 'nid');
324
}
325

    
326
/**
327
 * Implements hook_field_uuid_load().
328
 */
329
function user_reference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
330
  entity_property_id_to_uuid($items, 'user', 'uid');
331
}
332

    
333
/**
334
 * Implements hook_field_uuid_presave().
335
 */
336
function user_reference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
337
  entity_property_uuid_to_id($items, 'user', 'uid');
338
}
339

    
340
/**
341
 * Implements hook_field_uuid_load().
342
 */
343
function entityreference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
344
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
345
  // can't handle a single $item.
346
  entity_property_id_to_uuid($items, $field['settings']['target_type'], 'target_id');
347
}
348

    
349
/**
350
 * Implements hook_field_uuid_presave().
351
 */
352
function entityreference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
353
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
354
  // can't handle a single $item.
355
  entity_property_uuid_to_id($items, $field['settings']['target_type'], 'target_id');
356
}
357

    
358
/**
359
 * Implements hook_entity_uuid_load().
360
 */
361
function field_collection_entity_uuid_load(&$entities, $entity_type) {
362
  if ($entity_type == 'field_collection_item') {
363
    entity_property_id_to_uuid($entities, 'field_collection_item', 'value');
364
  }
365
}
366

    
367
/**
368
 * Implements hook_entity_uuid_presave().
369
 */
370
function field_collection_entity_uuid_presave(&$entity, $entity_type) {
371
  if ($entity_type == 'field_collection_item') {
372
    entity_property_uuid_to_id($entity, 'field_collection_item', 'value');
373
  }
374
}
375

    
376
/**
377
 * Implements hook_field_uuid_load().
378
 */
379
function field_collection_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
380
  entity_property_id_to_uuid($items, 'field_collection_item', 'value');
381
}
382

    
383
/**
384
 * Implements hook_field_uuid_presave().
385
 */
386
function field_collection_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
387
  entity_property_uuid_to_id($items, 'field_collection_item', 'value');
388
}
389

    
390
/**
391
 * @} End of "Field implementations"
392
 */
393

    
394
/**
395
 * @defgroup uuid_export Export alterations
396
 * @{
397
 */
398

    
399
/**
400
 * Implements hook_uuid_entities_features_export_entity_alter().
401
 */
402
function node_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
403
  if ($entity_type == 'node') {
404
    foreach (array('data', 'name', 'picture', 'revision_uid', 'last_comment_timestamp') as $property) {
405
      if (property_exists($entity, $property)) {
406
        unset($entity->{$property});
407
      }
408
    }
409
  }
410
}
411

    
412
/**
413
 * Implementations hook_uuid_entities_features_export_entity_alter().
414
 */
415
function user_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
416
  if ($entity_type == 'user') {
417
    foreach (array('data', 'access', 'login') as $property) {
418
      if (property_exists($entity, $property)) {
419
        unset($entity->{$property});
420
      }
421
    }
422
  }
423
}
424

    
425
/**
426
 * Implements hook_uuid_entities_features_export_alter().
427
 */
428
function file_uuid_entities_features_export_field_alter($entity_type, $entity, $field, $instance, $langcode, &$items) {
429
  foreach ($items as &$item) {
430
    if (isset($item['timestamp'])) {
431
      unset($item['timestamp']);
432
    }
433
  }
434
}
435

    
436
/**
437
 * Implements hook_uuid_entities_features_export_entity_alter().
438
 */
439
function workbench_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
440
  foreach (array('workbench_moderation', 'my_revision', 'workbench_access', 'workbench_access_scheme', 'workbench_access_by_role') as $property) {
441
    if (isset($entity->{$property})) {
442
      unset($entity->{$property});
443
    }
444
  }
445
}
446

    
447
/**
448
 * @} End of "Export alterations"
449
 */