1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Functions related to adding UUID support to embedded media.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_entity_uuid_load().
|
10
|
*/
|
11
|
function media_wysiwyg_entity_uuid_load(&$entities, $entity_type) {
|
12
|
// Go through all the entity's text fields and replace file IDs in media
|
13
|
// tokens with the corresponding UUID.
|
14
|
foreach ($entities as $entity) {
|
15
|
media_wysiwyg_filter_replace_tokens_in_all_text_fields($entity_type, $entity, 'media_wysiwyg_token_fid_to_uuid');
|
16
|
}
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Implements hook_entity_uuid_presave().
|
21
|
*/
|
22
|
function media_wysiwyg_entity_uuid_presave(&$entity, $entity_type) {
|
23
|
// Go through all the entity's text fields and replace UUIDs in media tokens
|
24
|
// with the corresponding file ID.
|
25
|
media_wysiwyg_filter_replace_tokens_in_all_text_fields($entity_type, $entity, 'media_wysiwyg_token_uuid_to_fid');
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Replaces media tokens in an entity's text fields, using the specified callback function.
|
30
|
*/
|
31
|
function media_wysiwyg_filter_replace_tokens_in_all_text_fields($entity_type, $entity, $callback) {
|
32
|
$text_field_names = media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity);
|
33
|
foreach ($text_field_names as $field_name) {
|
34
|
if (!empty($entity->{$field_name})) {
|
35
|
$field = field_info_field($field_name);
|
36
|
$all_languages = field_available_languages($entity_type, $field);
|
37
|
$field_languages = array_intersect($all_languages, array_keys($entity->{$field_name}));
|
38
|
foreach ($field_languages as $language) {
|
39
|
if (!empty($entity->{$field_name}[$language])) {
|
40
|
foreach ($entity->{$field_name}[$language] as &$item) {
|
41
|
$item['value'] = preg_replace_callback(MEDIA_WYSIWYG_TOKEN_REGEX, $callback, $item['value']);
|
42
|
}
|
43
|
}
|
44
|
}
|
45
|
}
|
46
|
}
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Callback to replace file IDs with UUIDs in a media token.
|
51
|
*/
|
52
|
function media_wysiwyg_token_fid_to_uuid($matches) {
|
53
|
return _media_wysiwyg_token_uuid_replace($matches, 'entity_get_uuid_by_id');
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Callback to replace UUIDs with file IDs in a media token.
|
58
|
*/
|
59
|
function media_wysiwyg_token_uuid_to_fid($matches) {
|
60
|
return _media_wysiwyg_token_uuid_replace($matches, 'entity_get_id_by_uuid');
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Helper function to replace UUIDs with file IDs or vice versa.
|
65
|
*
|
66
|
* @param array $matches
|
67
|
* An array of matches for media tokens, from a preg_replace_callback()
|
68
|
* callback function.
|
69
|
* @param string $entity_uuid_function
|
70
|
* Either 'entity_get_uuid_by_id' (to replace file IDs with UUIDs in the
|
71
|
* token) or 'entity_get_id_by_uuid' (to replace UUIDs with file IDs).
|
72
|
*
|
73
|
* @return string
|
74
|
* A string representing the JSON-encoded token, with the appropriate
|
75
|
* replacement between file IDs and UUIDs.
|
76
|
*/
|
77
|
function _media_wysiwyg_token_uuid_replace($matches, $entity_uuid_function) {
|
78
|
$tag = $matches[0];
|
79
|
$tag = str_replace(array('[[', ']]'), '', $tag);
|
80
|
$tag_info = drupal_json_decode($tag);
|
81
|
if (isset($tag_info['fid'])) {
|
82
|
if ($new_ids = $entity_uuid_function('file', array($tag_info['fid']))) {
|
83
|
$new_id = reset($new_ids);
|
84
|
$tag_info['fid'] = $new_id;
|
85
|
}
|
86
|
}
|
87
|
return '[[' . drupal_json_encode($tag_info) . ']]';
|
88
|
}
|