diff --git a/media.module b/media.module index d4ba669..e4242d6 100644 --- a/media.module +++ b/media.module @@ -1289,6 +1289,20 @@ function media_ctools_plugin_api($module, $api) { 'version' => 1, ); } + + // Override the fields of the file when requested by the WYSIWYG. + if (isset($file->override) && isset($file->override['fields'])) { + $instance = field_info_instances('file', $file->type); + foreach ($file->override['fields'] as $field_name => $field_values) { + if (!isset($instance[$field_name]['settings']) || !isset($instance[$field_name]['settings']['wysiwyg_override']) || $instance[$field_name]['settings']['wysiwyg_override']) { + foreach ($field_values as $langcode => $values) { + $file->{$field_name}[$langcode] = $values; + } + } + } + + file_entity_set_title_alt_properties([$file]); + } } /** diff --git a/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc b/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc index 79e3ba6..f11699f 100644 --- a/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc +++ b/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc @@ -211,10 +211,7 @@ function media_wysiwyg_token_to_markup($match, $wysiwyg = FALSE, $langcode = NUL } // Grab the potentially overridden fields from the file. - $fields = media_wysiwyg_filter_field_parser($tag_info); - foreach ($fields as $key => $value) { - $file->{$key} = $value; - } + $fields = media_wysiwyg_filter_field_parser($tag_info, $file); if (array_key_exists('attributes', $tag_info) && is_array($tag_info['attributes'])) { $attributes = $tag_info['attributes']; @@ -415,7 +412,7 @@ function media_wysiwyg_token_to_markup($match, $wysiwyg = FALSE, $langcode = NUL * @return array * An array of fields with sanitized field data structures. */ -function media_wysiwyg_filter_field_parser(array $tag_info) { +function media_wysiwyg_filter_field_parser(array $tag_info, $file) { $fields = array(); if (isset($tag_info['fields'])) { // Field value reference candidates (keys) that end in [format] are @@ -456,25 +453,20 @@ function media_wysiwyg_filter_field_parser(array $tag_info) { $parsed_field[3] = key($info['columns']); } - // Each key of the field needs to be the child of the previous key. - $ref = &$fields; - foreach ($parsed_field as $key) { - if (!isset($ref[$key])) { - $ref[$key] = array(); - } - $ref = &$ref[$key]; - } - // The value should be set at the deepest level. - if (in_array($candidate, $url_encoded_fields)) { - // Fields that use rich-text markup will be urlencoded. - $ref = urldecode(decode_entities($field_value)); + $parents = $parsed_field; + // $fields collects these overrides. + drupal_array_set_nested_value($fields, $parents, $field_value); + + $field_name = array_shift($parents); + if (!empty($field_value)) { + drupal_array_set_nested_value($file->$field_name, $parents, $field_value); } else { - // Only entities need to be decoded. - $ref = decode_entities($field_value); + $file->$field_name = NULL; } } } + // Strip fields for empty values. This is necessary to do post parsing of // all field entries as they may refer to the same field several times. foreach ($fields as $field_name => $field_languages) { diff --git a/modules/media_wysiwyg/media_wysiwyg.module b/modules/media_wysiwyg/media_wysiwyg.module index 4777736..409bb25 100644 --- a/modules/media_wysiwyg/media_wysiwyg.module +++ b/modules/media_wysiwyg/media_wysiwyg.module @@ -444,6 +444,102 @@ function media_wysiwyg_get_file_without_label($file, $view_mode, $settings = arr } } + // For the "alt" and "title" attributes, there are many ways an attribute + // might be overridden within the WYSIWYG. (For example, the attribute may + // have been edited directly via built-in WYISWYG functionality, or it may + // have been rendered as part of the file display mode that was used when + // embedding the image.) This is problematic for translation because there is + // no indication of what language the overridden "alt" and "title" attributes + // were intended to be in. + // + // To remedy this, before using the overridden "alt" or "title" attribute + // value, this code checks what value the file entity has for "alt" and + // "title" in the current display language. If the override is different from + // the current display value, and if it matches the corresponding overridden + // field value for a different language, then it is assumed that it is + // associated with that language instead, and it is not used as an override. + // + // For example, if the settings look like this: + // + // @code + // $settings = array( + // 'attributes' => array( + // 'alt' => 'Text in English', + // ), + // 'fields' => array( + // 'field_file_image_alt_text' => array( + // 'en' => array( + // 0 => array( + // 'value' => 'Text in English', + // ), + // ), + // ), + // ), + // ); + // @endcode + // + // then it can be inferred that the overridden alt text is also in English. + // If rendering the file in Spanish produces a different value for the alt + // text (for example, "Texto en Español"), then the override should not be + // used when the file is being viewed in Spanish. + if (!empty($settings['fields'])) { + $token_patterns = array(); + if (!empty($settings['attributes']['alt'])) { + $token_patterns['alt'] = variable_get('file_entity_alt', '[file:field_file_image_alt_text]'); + } + if (!empty($settings['attributes']['title'])) { + $token_patterns['title'] = variable_get('file_entity_title', '[file:field_file_image_title_text]'); + } + foreach (array_filter($token_patterns) as $attribute => $token_pattern) { + // Go through each field override and check if it corresponds to the + // token pattern for the "alt" or "title" attribute. + foreach ($settings['fields'] as $field_name => $fields_by_language) { + // @todo This assumes a particular token pattern that may not be valid. + if ($token_pattern == "[file:$field_name]") { + // Find the display value of this field in the current display + // language, using the overrides for this field that are stored in + // the settings. + $file_with_overrides = clone $file; + foreach ($fields_by_language as $field_langcode => $field_data) { + $file_with_overrides->{$field_name}[$field_langcode] = $field_data; + } + $text = token_replace($token_pattern, array('file' => $file_with_overrides), array('clear' => TRUE, 'sanitize' => FALSE)); + // If the overridden attribute value does not match the current + // display value of the field, proceed to check if it matches a + // different language instead. + if ($text != $settings['attributes'][$attribute]) { + $langcodes = array_keys($fields_by_language); + if ($langcodes) { + // Leave off the current display language which was used above. + $field_language = field_language('file', $file_with_overrides, $field_name); + $langcodes = array_diff($langcodes, array($field_language)); + } + if ($langcodes) { + // Treat LANGUAGE_NONE as a valid language here, since the + // overridden field value that appears in the embedded media + // macro might use it. + $languages = language_list() + array(LANGUAGE_NONE => language_default()); + foreach ($langcodes as $langcodes_langcode) { + if (isset($languages[$langcodes_langcode])) { + // Check the display value of the field in the candidate + // language. + $text = token_replace($token_pattern, array('file' => $file_with_overrides), array('clear' => TRUE, 'sanitize' => FALSE, 'language' => $languages[$langcodes_langcode])); + // If it matches the overridden attribute value, assume the + // attribute override was for that language, and don't use it + // here. + if ($text == $settings['attributes'][$attribute]) { + unset($settings['attributes'][$attribute]); + break 2; + } + } + } + } + } + } + } + } + } + $file->override = $settings; $element = file_view_file($file, $view_mode, $langcode);