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
|
// A node always must have an author.
|
37
|
if (empty($entity->uid)) {
|
38
|
global $user;
|
39
|
$entity->uid = $user->uid;
|
40
|
}
|
41
|
}
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Implements hook_entity_uuid_save().
|
46
|
*/
|
47
|
function node_entity_uuid_save(&$entity, $entity_type) {
|
48
|
/*
|
49
|
* When a node is translated, the source node's tnid is set to it's own nid.
|
50
|
* When deploying the node for the first time the tnid can't be translated
|
51
|
* to an nid until after the node has been saved. So if the entity's tnid
|
52
|
* is still a uuid at this point it needs to be translated to an nid.
|
53
|
*/
|
54
|
if ($entity_type == 'node' && uuid_is_valid($entity->tnid)) {
|
55
|
entity_property_uuid_to_id($entity, 'node', 'tnid');
|
56
|
db_update('node')
|
57
|
->fields(array('tnid' => $entity->tnid))
|
58
|
->condition('nid', $entity->nid)
|
59
|
->execute();
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Implements hook_entity_uuid_load().
|
65
|
*/
|
66
|
function book_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
|
67
|
if ($entity_type == 'node') {
|
68
|
if (!empty($entity->book)) {
|
69
|
$entity->book['bid'] = current(entity_get_uuid_by_id($entity_type, array($entity->book['bid'])));
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Implements hook_entity_uuid_presave().
|
76
|
*/
|
77
|
function book_entity_uuid_presave(&$entity, $entity_type) {
|
78
|
if ($entity_type == 'node') {
|
79
|
if (!empty($entity->book)) {
|
80
|
$entity->book['bid'] = current(entity_get_id_by_uuid($entity_type, array($entity->book['bid'])));
|
81
|
if (!$entity->book['bid']) {
|
82
|
$entity->book['bid'] = 'new';
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Implements hook_entity_uuid_presave().
|
90
|
*/
|
91
|
function user_entity_uuid_presave(&$entity, $entity_type) {
|
92
|
if ($entity_type != 'user') {
|
93
|
return;
|
94
|
}
|
95
|
|
96
|
/*
|
97
|
* We need to ensure new user's passwords are encrypted. The Services module
|
98
|
* transparently encrypts the password for new users. md5() is used by
|
99
|
* users who's accounts were migrated from Drupal 6 and who haven't updated
|
100
|
* their password.
|
101
|
*/
|
102
|
if (isset($entity->pass)
|
103
|
&& (!('$S$D' == substr($entity->pass, 0, 4)) || preg_match('/^[a-f0-9]{32}$/', $entity->pass))) {
|
104
|
// Ensure user's password is hashed.
|
105
|
$entity->pass = user_hash_password($entity->pass);
|
106
|
}
|
107
|
|
108
|
if (empty($entity->picture)) {
|
109
|
return;
|
110
|
}
|
111
|
|
112
|
$uuids = entity_get_id_by_uuid('file', array($entity->picture['uuid']));
|
113
|
$fid = current($uuids);
|
114
|
if (!$entity->is_new) {
|
115
|
$entity->picture = file_load($fid);
|
116
|
}
|
117
|
else {
|
118
|
$entity->picture = $fid;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Implements hook_entity_uuid_load().
|
124
|
*/
|
125
|
function comment_entity_uuid_load(&$entities, $entity_type) {
|
126
|
switch ($entity_type) {
|
127
|
case 'node':
|
128
|
entity_property_id_to_uuid($entities, 'user', 'last_comment_uid');
|
129
|
break;
|
130
|
|
131
|
case 'comment':
|
132
|
entity_property_id_to_uuid($entities, 'user', array('uid', 'u_uid'));
|
133
|
entity_property_id_to_uuid($entities, 'node', 'nid');
|
134
|
break;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Implements hook_entity_uuid_presave().
|
140
|
*/
|
141
|
function comment_entity_uuid_presave(&$entity, $entity_type) {
|
142
|
switch ($entity_type) {
|
143
|
case 'node':
|
144
|
entity_property_uuid_to_id($entity, 'user', 'last_comment_uid');
|
145
|
break;
|
146
|
|
147
|
case 'comment':
|
148
|
// entity_make_entity_local() may have unset cid, add back if necessary.
|
149
|
if (!isset($entity->cid)) {
|
150
|
$entity->cid = NULL;
|
151
|
}
|
152
|
entity_property_uuid_to_id($entity, 'user', array('uid', 'u_uid'));
|
153
|
entity_property_uuid_to_id($entity, 'node', 'nid');
|
154
|
break;
|
155
|
}
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Implements hook_entity_uuid_load().
|
160
|
*/
|
161
|
function file_entity_uuid_load(&$entities, $entity_type) {
|
162
|
if ($entity_type == 'file') {
|
163
|
entity_property_id_to_uuid($entities, 'user', 'uid');
|
164
|
}
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* Implements hook_entity_uuid_presave().
|
169
|
*/
|
170
|
function file_entity_uuid_presave(&$entity, $entity_type) {
|
171
|
if ($entity_type == 'file') {
|
172
|
// entity_make_entity_local() may have unset fid, add back if necessary.
|
173
|
if (!isset($entity->fid)) {
|
174
|
$entity->fid = NULL;
|
175
|
}
|
176
|
entity_property_uuid_to_id($entity, 'user', 'uid');
|
177
|
|
178
|
// Write the new file to the local filesystem.
|
179
|
if (isset($entity->file_contents) && !empty($entity->filesize)) {
|
180
|
// Don't try to write it if it uses a stream wrapper that isn't writeable
|
181
|
// (for example, if it is a remotely-hosted video).
|
182
|
$scheme = file_uri_scheme($entity->uri);
|
183
|
$wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
|
184
|
if (empty($wrappers[$scheme])) {
|
185
|
return;
|
186
|
}
|
187
|
|
188
|
// Check for an existing file with the same URI.
|
189
|
$existing_files = file_load_multiple(array(), array('uri' => $entity->uri));
|
190
|
$existing = (object) array('uri' => NULL, 'uuid' => NULL);
|
191
|
if (count($existing_files)) {
|
192
|
$existing = reset($existing_files);
|
193
|
}
|
194
|
|
195
|
// If this is a new file and there is an existing file with the same URI,
|
196
|
// but a different uuid then rename this file.
|
197
|
if ($entity->is_new && $entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
|
198
|
$uri = $entity->uri;
|
199
|
$replace = FILE_EXISTS_RENAME;
|
200
|
}
|
201
|
// If this has an id, meaning UUID has already matched the uuid to an
|
202
|
// existing file, but it has a URI that matches a file with a different
|
203
|
// uuid, then load the file with the matching uuid and use the URI from
|
204
|
// that file. The existing file with the matching uuid is most likely a
|
205
|
// file that was previously renamed, e.g. as in the condition above, to
|
206
|
// avoid conflict. The uuid matches because they are the same file, but
|
207
|
// the URI does not because an incrementing number was added as part of
|
208
|
// the renaming.
|
209
|
elseif ($entity->uri == $existing->uri && $entity->uuid != $existing->uuid) {
|
210
|
$file = file_load($entity->fid);
|
211
|
$uri = $file->uri;
|
212
|
$replace = FILE_EXISTS_REPLACE;
|
213
|
}
|
214
|
// Otherwise create a new file or replace the existing file contents.
|
215
|
else {
|
216
|
$uri = $entity->uri;
|
217
|
$replace = FILE_EXISTS_REPLACE;
|
218
|
}
|
219
|
|
220
|
$directory = drupal_dirname($uri);
|
221
|
if (!empty($directory) && file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
|
222
|
$entity->uri = file_unmanaged_save_data(base64_decode($entity->file_contents), $uri, $replace);
|
223
|
}
|
224
|
}
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Implements hook_entity_uuid_load().
|
230
|
*/
|
231
|
function taxonomy_entity_uuid_load(&$entities, $entity_type) {
|
232
|
if ($entity_type == 'taxonomy_term') {
|
233
|
foreach ($entities as &$entity) {
|
234
|
if (isset($entity->parent)) {
|
235
|
if (!is_array($entity->parent)) {
|
236
|
$entity->parent = array($entity->parent);
|
237
|
}
|
238
|
$uuids = entity_get_uuid_by_id('taxonomy_term', $entity->parent);
|
239
|
$entity->parent = array_values($uuids);
|
240
|
}
|
241
|
unset($entity->vid);
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Implements hook_entity_uuid_presave().
|
248
|
*/
|
249
|
function taxonomy_entity_uuid_presave(&$entity, $entity_type) {
|
250
|
if ($entity_type == 'taxonomy_term') {
|
251
|
if (isset($entity->parent)) {
|
252
|
if (!is_array($entity->parent)) {
|
253
|
$entity->parent = array($entity->parent);
|
254
|
}
|
255
|
$ids = entity_get_id_by_uuid('taxonomy_term', $entity->parent);
|
256
|
$entity->parent = array_values($ids);
|
257
|
}
|
258
|
$vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name);
|
259
|
$entity->vid = $vocabulary->vid;
|
260
|
}
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Implements hook_entity_uuid_load().
|
265
|
*/
|
266
|
function field_entity_uuid_load(&$entities, $entity_type) {
|
267
|
foreach ($entities as $entity) {
|
268
|
list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
|
269
|
$instances = field_info_instances($entity_type, $bundle_name);
|
270
|
|
271
|
foreach ($instances as $field_name => $instance) {
|
272
|
$field = field_info_field($field_name);
|
273
|
if (!empty($field) && isset($entity->{$field_name})) {
|
274
|
foreach ($entity->{$field_name} as $langcode => &$items) {
|
275
|
// Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
|
276
|
// that is not passing by reference.
|
277
|
$function = $field['module'] . '_field_uuid_load';
|
278
|
if (function_exists($function)) {
|
279
|
$function($entity_type, $entity, $field, $instance, $langcode, $items);
|
280
|
}
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Implements hook_entity_uuid_presave().
|
289
|
*/
|
290
|
function field_entity_uuid_presave(&$entity, $entity_type) {
|
291
|
list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
|
292
|
$instances = field_info_instances($entity_type, $bundle_name);
|
293
|
|
294
|
foreach ($instances as $field_name => $instance) {
|
295
|
$field = field_info_field($field_name);
|
296
|
if (!empty($field) && isset($entity->{$field_name})) {
|
297
|
foreach ($entity->{$field_name} as $langcode => &$items) {
|
298
|
// Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
|
299
|
// that is not passing by reference.
|
300
|
$function = $field['module'] . '_field_uuid_presave';
|
301
|
if (function_exists($function)) {
|
302
|
$function($entity_type, $entity, $field, $instance, $langcode, $items);
|
303
|
}
|
304
|
}
|
305
|
}
|
306
|
}
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* @} End of "Property implementations"
|
311
|
*/
|
312
|
|
313
|
/**
|
314
|
* @defgroup uuid_field Field implementations
|
315
|
* @{
|
316
|
*/
|
317
|
|
318
|
/**
|
319
|
* Implements hook_field_uuid_load().
|
320
|
*/
|
321
|
function taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
322
|
entity_property_id_to_uuid($items, 'taxonomy_term', 'tid');
|
323
|
}
|
324
|
|
325
|
/**
|
326
|
* Implements hook_field_uuid_presave().
|
327
|
*/
|
328
|
function taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
329
|
entity_property_uuid_to_id($items, 'taxonomy_term', 'tid');
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* Implements hook_field_uuid_load().
|
334
|
*/
|
335
|
function file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
336
|
entity_property_id_to_uuid($items, 'file', 'fid');
|
337
|
entity_property_id_to_uuid($items, 'user', 'uid');
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Implements hook_field_uuid_presave().
|
342
|
*/
|
343
|
function file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
344
|
entity_property_uuid_to_id($items, 'file', 'fid');
|
345
|
entity_property_uuid_to_id($items, 'user', 'uid');
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* Implements hook_field_uuid_load().
|
350
|
*/
|
351
|
function image_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
352
|
file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* Implements hook_field_uuid_presave().
|
357
|
*/
|
358
|
function image_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
359
|
file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
|
360
|
}
|
361
|
|
362
|
/**
|
363
|
* Implements hook_field_uuid_load().
|
364
|
*
|
365
|
* Kept here because it is in D8 core.
|
366
|
*/
|
367
|
function entityreference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
368
|
// TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
|
369
|
// can't handle a single $item.
|
370
|
entity_property_id_to_uuid($items, $field['settings']['target_type'], 'target_id');
|
371
|
}
|
372
|
|
373
|
/**
|
374
|
* Implements hook_field_uuid_presave().
|
375
|
*
|
376
|
* Kept here because it is in D8 core.
|
377
|
*/
|
378
|
function entityreference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
379
|
// TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
|
380
|
// can't handle a single $item.
|
381
|
entity_property_uuid_to_id($items, $field['settings']['target_type'], 'target_id');
|
382
|
}
|
383
|
|
384
|
/**
|
385
|
* @} End of "Field implementations"
|
386
|
*/
|
387
|
|
388
|
/**
|
389
|
* @defgroup uuid_export Export alterations
|
390
|
* @{
|
391
|
*/
|
392
|
|
393
|
/**
|
394
|
* Implements hook_uuid_entities_features_export_entity_alter().
|
395
|
*/
|
396
|
function node_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
|
397
|
if ('node' != $entity_type) {
|
398
|
return;
|
399
|
}
|
400
|
|
401
|
$properties = array(
|
402
|
'data',
|
403
|
'name',
|
404
|
'picture',
|
405
|
'revision_uid',
|
406
|
'last_comment_timestamp',
|
407
|
);
|
408
|
foreach ($properties as $property) {
|
409
|
if (property_exists($entity, $property)) {
|
410
|
unset($entity->{$property});
|
411
|
}
|
412
|
}
|
413
|
}
|
414
|
|
415
|
/**
|
416
|
* Implements hook_uuid_entities_features_export_entity_alter().
|
417
|
*/
|
418
|
function user_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
|
419
|
if ('user' != $entity_type) {
|
420
|
return;
|
421
|
}
|
422
|
|
423
|
foreach (array('data', 'access', 'login') as $property) {
|
424
|
if (property_exists($entity, $property)) {
|
425
|
unset($entity->{$property});
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* Implements hook_uuid_entities_features_export_alter().
|
432
|
*/
|
433
|
function file_uuid_entities_features_export_field_alter($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
434
|
foreach ($items as &$item) {
|
435
|
if (isset($item['timestamp'])) {
|
436
|
unset($item['timestamp']);
|
437
|
}
|
438
|
}
|
439
|
}
|
440
|
|
441
|
/**
|
442
|
* @} End of "Export alterations"
|
443
|
*/
|