summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorunknown2013-08-08 16:53:36 (GMT)
committer unknown2013-08-08 16:53:36 (GMT)
commit13125f1ffe827a5dee5528142466b7a758b4b4c6 (patch)
tree7ed9b7dce446e1d42de212794a92b061a4c197d2
parent616993459393210da8a421c472fed754787d93ff (diff)
issue #1578108 by RoSk0, David_Rothstein, sylus: Support UUID/Deploy for files that are referenced using the WYSIWYG in a textarea
-rw-r--r--includes/media.filter.inc95
1 files changed, 95 insertions, 0 deletions
diff --git a/includes/media.filter.inc b/includes/media.filter.inc
index a17d7eb..24ff511 100644
--- a/includes/media.filter.inc
+++ b/includes/media.filter.inc
@@ -752,3 +752,98 @@ function media_filter_track_usage($fid) {
db_merge('media_filter_usage')->key(array('fid' => $fid))->fields(array('fid' => $fid, 'timestamp' => REQUEST_TIME))->execute();
}
}
+
+/**
+ * Implements hook_entity_dependencies().
+ */
+function media_entity_dependencies($entity, $entity_type) {
+ // Go through all the entity's text fields and add a dependency on any files
+ // that are referenced there.
+ $dependencies = array();
+ foreach (media_filter_parse_from_fields($entity_type, $entity) as $file_reference) {
+ $dependencies[] = array('type' => 'file', 'id' => $file_reference['fid']);
+ }
+ return $dependencies;
+}
+
+/**
+ * Implements hook_entity_uuid_load().
+ */
+function media_entity_uuid_load(&$entities, $entity_type) {
+ // Go through all the entity's text fields and replace file IDs in media
+ // tokens with the corresponding UUID.
+ foreach ($entities as $entity) {
+ media_filter_replace_tokens_in_all_text_fields($entity_type, $entity, 'media_token_fid_to_uuid');
+ }
+}
+
+/**
+ * Implements hook_entity_uuid_presave().
+ */
+function media_entity_uuid_presave(&$entity, $entity_type) {
+ // Go through all the entity's text fields and replace UUIDs in media tokens
+ // with the corresponding file ID.
+ media_filter_replace_tokens_in_all_text_fields($entity_type, $entity, 'media_token_uuid_to_fid');
+}
+
+/**
+ * Replaces media tokens in an entity's text fields, using the specified callback function.
+ */
+function media_filter_replace_tokens_in_all_text_fields($entity_type, $entity, $callback) {
+ $text_field_names = _media_filter_fields_with_text_filtering($entity_type, $entity);
+ foreach ($text_field_names as $field_name) {
+ if (!empty($entity->{$field_name})) {
+ $field = field_info_field($field_name);
+ $all_languages = field_available_languages($entity_type, $field);
+ $field_languages = array_intersect($all_languages, array_keys($entity->{$field_name}));
+ foreach ($field_languages as $language) {
+ if (!empty($entity->{$field_name}[$language])) {
+ foreach ($entity->{$field_name}[$language] as &$item) {
+ $item['value'] = preg_replace_callback(MEDIA_TOKEN_REGEX, $callback, $item['value']);
+ }
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Callback to replace file IDs with UUIDs in a media token.
+ */
+function media_token_fid_to_uuid($matches) {
+ return _media_token_uuid_replace($matches, 'entity_get_uuid_by_id');
+}
+
+/**
+ * Callback to replace UUIDs with file IDs in a media token.
+ */
+function media_token_uuid_to_fid($matches) {
+ return _media_token_uuid_replace($matches, 'entity_get_id_by_uuid');
+}
+
+/**
+ * Helper function to replace UUIDs with file IDs or vice versa.
+ *
+ * @param array $matches
+ * An array of matches for media tokens, from a preg_replace_callback()
+ * callback function.
+ * @param string $entity_uuid_function
+ * Either 'entity_get_uuid_by_id' (to replace file IDs with UUIDs in the
+ * token) or 'entity_get_id_by_uuid' (to replace UUIDs with file IDs).
+ *
+ * @return string
+ * A string representing the JSON-encoded token, with the appropriate
+ * replacement between file IDs and UUIDs.
+ */
+function _media_token_uuid_replace($matches, $entity_uuid_function) {
+ $tag = $matches[0];
+ $tag = str_replace(array('[[', ']]'), '', $tag);
+ $tag_info = drupal_json_decode($tag);
+ if (isset($tag_info['fid'])) {
+ if ($new_ids = $entity_uuid_function('file', array($tag_info['fid']))) {
+ $new_id = reset($new_ids);
+ $tag_info['fid'] = $new_id;
+ }
+ }
+ return '[[' . drupal_json_encode($tag_info) . ']]';
+}