Projet

Général

Profil

Paste
Télécharger (11,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid.core.inc @ 87dbc3bf

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
    if (isset($entity->file_contents)) {
129
      $directory = drupal_dirname($entity->uri);
130
      file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
131
      file_unmanaged_save_data(base64_decode($entity->file_contents), $entity->uri, FILE_EXISTS_REPLACE);
132
    }
133
  }
134
}
135

    
136
/**
137
 * Implements hook_entity_uuid_load().
138
 */
139
function taxonomy_entity_uuid_load(&$entities, $entity_type) {
140
  if ($entity_type == 'taxonomy_term') {
141
    foreach ($entities as &$entity) {
142
      if (isset($entity->parent)) {
143
        if (!is_array($entity->parent)) {
144
          $entity->parent = array($entity->parent);
145
        }
146
        $uuids = entity_get_uuid_by_id('taxonomy_term', $entity->parent);
147
        $entity->parent = array_values($uuids);
148
      }
149
      unset($entity->vid);
150
    }
151
  }
152
}
153

    
154
/**
155
 * Implements hook_entity_uuid_presave().
156
 */
157
function taxonomy_entity_uuid_presave(&$entity, $entity_type) {
158
  if ($entity_type == 'taxonomy_term') {
159
    if (isset($entity->parent)) {
160
      if (!is_array($entity->parent)) {
161
        $entity->parent = array($entity->parent);
162
      }
163
      $ids = entity_get_id_by_uuid('taxonomy_term', $entity->parent);
164
      $entity->parent = array_values($ids);
165
    }
166
    $vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name);
167
    $entity->vid = $vocabulary->vid;
168
  }
169
}
170

    
171
/**
172
 * Implements hook_entity_uuid_load().
173
 */
174
function field_entity_uuid_load(&$entities, $entity_type) {
175
  foreach ($entities as $i => $entity) {
176
    list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
177
    $instances = field_info_instances($entity_type, $bundle_name);
178

    
179
    foreach ($instances as $field_name => $instance) {
180
      $field = field_info_field($field_name);
181
      if (!empty($field) && isset($entity->{$field_name})) {
182
        foreach ($entity->{$field_name} as $langcode => &$items) {
183
          // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
184
          // that is not passing by reference.
185
          $function = $field['module'] . '_field_uuid_load';
186
          if (function_exists($function)) {
187
            $function($entity_type, $entity, $field, $instance, $langcode, $items);
188
          }
189
        }
190
      }
191
    }
192
  }
193
}
194

    
195
/**
196
 * Implements hook_entity_uuid_presave().
197
 */
198
function field_entity_uuid_presave(&$entity, $entity_type) {
199
  list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
200
  $instances = field_info_instances($entity_type, $bundle_name);
201

    
202
  foreach ($instances as $field_name => $instance) {
203
    $field = field_info_field($field_name);
204
    if (!empty($field) && isset($entity->{$field_name})) {
205
      foreach ($entity->{$field_name} as $langcode => &$items) {
206
        // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
207
        // that is not passing by reference.
208
        $function = $field['module'] . '_field_uuid_presave';
209
        if (function_exists($function)) {
210
          $function($entity_type, $entity, $field, $instance, $langcode, $items);
211
        }
212
      }
213
    }
214
  }
215
}
216

    
217
/**
218
 * @} End of "Property implementations"
219
 */
220

    
221
/**
222
 * @defgroup uuid_field Field implementations
223
 * @{
224
 */
225

    
226
/**
227
 * Implements hook_field_uuid_load().
228
 */
229
function taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
230
  entity_property_id_to_uuid($items, 'taxonomy_term', 'tid');
231
}
232

    
233
/**
234
 * Implements hook_field_uuid_presave().
235
 */
236
function taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
237
  entity_property_uuid_to_id($items, 'taxonomy_term', 'tid');
238
}
239

    
240
/**
241
 * Implements hook_field_uuid_load().
242
 */
243
function file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
244
  entity_property_id_to_uuid($items, 'file', 'fid');
245
  entity_property_id_to_uuid($items, 'user', 'uid');
246
}
247

    
248
/**
249
 * Implements hook_field_uuid_presave().
250
 */
251
function file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
252
  entity_property_uuid_to_id($items, 'file', 'fid');
253
  entity_property_uuid_to_id($items, 'user', 'uid');
254
}
255

    
256
/**
257
 * Implements hook_field_uuid_load().
258
 */
259
function image_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
260
  file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
261
}
262

    
263
/**
264
 * Implements hook_field_uuid_presave().
265
 */
266
function image_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
267
  file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
268
}
269

    
270
/**
271
 * Implements hook_field_uuid_load().
272
 */
273
function node_reference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
274
  entity_property_id_to_uuid($items, 'node', 'nid');
275
}
276

    
277
/**
278
 * Implements hook_field_uuid_presave().
279
 */
280
function node_reference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
281
  entity_property_uuid_to_id($items, 'node', 'nid');
282
}
283

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

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

    
298
/**
299
 * Implements hook_field_uuid_load().
300
 */
301
function entityreference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
302
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
303
  // can't handle a single $item.
304
  entity_property_id_to_uuid($items, $field['settings']['target_type'], 'target_id');
305
}
306

    
307
/**
308
 * Implements hook_field_uuid_presave().
309
 */
310
function entityreference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
311
  // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
312
  // can't handle a single $item.
313
  entity_property_uuid_to_id($items, $field['settings']['target_type'], 'target_id');
314
}
315

    
316
/**
317
 * Implements hook_entity_uuid_load().
318
 */
319
function field_collection_entity_uuid_load(&$entities, $entity_type) {
320
  if ($entity_type == 'field_collection_item') {
321
    entity_property_id_to_uuid($entities, 'field_collection_item', 'value');
322
  }
323
}
324

    
325
/**
326
 * Implements hook_entity_uuid_presave().
327
 */
328
function field_collection_entity_uuid_presave(&$entity, $entity_type) {
329
  if ($entity_type == 'field_collection_item') {
330
    entity_property_uuid_to_id($entity, 'field_collection_item', 'value');
331
  }
332
}
333

    
334
/**
335
 * Implements hook_field_uuid_load().
336
 */
337
function field_collection_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
338
  entity_property_id_to_uuid($items, 'field_collection_item', 'value');
339
}
340

    
341
/**
342
 * Implements hook_field_uuid_presave().
343
 */
344
function field_collection_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
345
  entity_property_uuid_to_id($items, 'field_collection_item', 'value');
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
 * Implementation of 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
 * Implements hook_uuid_entities_features_export_entity_alter().
396
 */
397
function workbench_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
398
  foreach (array('workbench_moderation', 'my_revision', 'workbench_access', 'workbench_access_scheme', 'workbench_access_by_role') as $property) {
399
    if (isset($entity->{$property})) {
400
      unset($entity->{$property});
401
    }
402
  }
403
}
404

    
405
/**
406
 * @} End of "Export alterations"
407
 */