diff --git a/metatag.module b/metatag.module index c24e291..4ceb428 100644 --- a/metatag.module +++ b/metatag.module @@ -373,10 +373,9 @@ function metatag_metatags_load_multiple($type, array $ids) { * The language of the translation set */ function metatag_metatags_save($type, $id, $metatags, $language) { - // Use the default content language if the entity doesn't have language - // support. + // If no language assigned, use the has-no-language language. if (!$language) { - $language = $GLOBALS['language_content']->language; + $language = LANGUAGE_NONE; } // Check that $id is numeric because of Entity API and string IDs. @@ -522,8 +521,7 @@ function metatag_entity_insert($entity, $entity_type) { list($id) = entity_extract_ids($entity_type, $entity); // Determine the language as per http://drupal.org/node/1626346. - $language = function_exists('entity_language') ? - entity_language($entity_type, $entity) : $entity->language; + $language = metatag_entity_get_language($entity_type, $entity); metatag_metatags_save($entity_type, $id, $entity->metatags, $language); } @@ -536,12 +534,29 @@ function metatag_entity_update($entity, $entity_type) { list($id) = entity_extract_ids($entity_type, $entity); if (isset($entity->metatags)) { + // Determine the language for this entity object. + $new_language = metatag_entity_get_language($entity_type, $entity); - // Determine the language as per http://drupal.org/node/1626346. - $language = function_exists('entity_language') ? - entity_language($entity_type, $entity) : $entity->language; + // If Entity Translation is enabled and this is a new translation, don't + // touch the existing data. + if (module_exists('entity_translation') && isset($entity->translation)) { + // Do nothing. + } + // Otherwise, see if an old record should be purged. + else { + // The old version of the entity should be available as $entity->original + // and then it may have a language value, if this is different to the + // current language then remove the old one. + if (isset($entity->original) && isset($entity->original->language) && $entity->original->language != $new_language) { + db_delete('metatag') + ->condition('entity_type', $entity_type) + ->condition('entity_id', $id) + ->condition('language', $entity->original->language) + ->execute(); + } + } - metatag_metatags_save($entity_type, $id, $entity->metatags, $language); + metatag_metatags_save($entity_type, $id, $entity->metatags, $new_language); } else { // Still ensure the meta tag output is cached. @@ -587,6 +602,11 @@ function metatag_entity_view($entity, $entity_type, $view_mode, $langcode) { return; } + // Possibly override the language selection. + if (metatag_entity_get_language($entity_type, $entity) == LANGUAGE_NONE) { + $langcode = LANGUAGE_NONE; + } + list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity); $instance = "{$entity_type}:{$bundle}"; @@ -609,7 +629,7 @@ function metatag_entity_view($entity, $entity_type, $view_mode, $langcode) { // hook_metatag_page_cache_cid_parts_alter(). drupal_alter('metatag_page_cache_cid_parts', $cid_parts); - $cid = "output:{$entity_type}:{$entity_id}:" . hash('sha256', serialize($cid_parts)); + $cid = "output:{$entity_type}:{$entity_id}:{$langcode}:" . hash('sha256', serialize($cid_parts)); if ($cache = cache_get($cid, 'cache_metatag')) { $entity->content['metatags'] = $cache->data; @@ -626,7 +646,7 @@ function metatag_entity_view($entity, $entity_type, $view_mode, $langcode) { // Ensure we actually pass a language object rather than language code. $languages = language_list(); - if (isset($langcode) && isset($languages[$langcode])) { + if (isset($languages[$langcode])) { $options['language'] = $languages[$langcode]; } @@ -672,25 +692,11 @@ function metatag_metatags_view($instance, array $metatags = array(), array $opti // If there are any tags, determine the translation to display. if (!empty($metatags)) { - // Get the display language. - if (isset($options['language']->language)) { - // Use the passed-in option. - $translation = $options['language']->language; - } - elseif (isset($metatags[$GLOBALS['language_content']->language])) { - // We weren't given a language; use the global content one. - $translation = $GLOBALS['language_content']->language; - } - else { - // The language is not defined. - $translation = LANGUAGE_NONE; - } - - // Choose the derived translation, fail over to the language-agnostic - // values if applicable. - if (!empty($metatags[$translation])) { - $metatags = $metatags[$translation]; + // Get the display language; default to the entity's language. + if (isset($options['language']) && isset($options['language']->language) && isset($metatags[$options['language']->language])) { + $metatags = $metatags[$options['language']->language]; } + // If no language requested, use the no-language value. elseif (!empty($metatags[LANGUAGE_NONE])) { $metatags = $metatags[LANGUAGE_NONE]; } @@ -1147,40 +1153,32 @@ function metatag_field_attach_form($entity_type, $entity, &$form, &$form_state, $instance = "{$entity_type}:{$bundle}"; // Grab the meta tags for display in the form if there are any. - if (isset($entity->metatags)) { - - // Determine the entity language as per http://drupal.org/node/1626346. - $entity_language = function_exists('entity_language') ? - entity_language($entity_type, $entity) : $entity->language; + if (!empty($entity->metatags)) { + // Identify the language to use with this entity. + $entity_language = metatag_entity_get_language($entity_type, $entity); // Determine from where we should get the tags. - if (!(isset($entity->metatags[$langcode]) || isset($entity->metatags[$entity_language]))) { - - // This is a preview so set the tags to the raw submission data. No - // language has been set. - $metatags = $entity->metatags; - } - elseif (isset($entity->metatags[$langcode])) { + if (isset($entity->metatags[$langcode])) { // Set the tags to the translation set matching that of the form. $metatags = $entity->metatags[$langcode]; - - // For tags that aren't set in the current form language, fill them in - // with default data from the original translation, the entity language. - if (isset($entity->metatags[$entity_language])) { - foreach ($entity->metatags[$entity_language] as $tag_id => $tag_data) { - if (!isset($metatags[$tag_id])) { - $metatags[$tag_id] = $tag_data; - } - } - } } // There is no translation for this entity's tags in the current // language. Instead, display tags in the language of the entity, the // source language of translations. The will provide translators with the // original text to translate. - else { + elseif (isset($entity->metatags[$entity_language])) { $metatags = $entity->metatags[$entity_language]; } + // When translating an existing entity, copy meta tags from the original + // language. + elseif (isset($entity->translations) && isset($entity->translations->original) && isset($entity->metatags[$entity->translations->original])) { + $metatags = $entity->metatags[$entity->translations->original]; + } + // This is a preview so set the tags to the raw submission data. No + // language has been set. + else { + $metatags = $entity->metatags; + } } else { $metatags = array(); @@ -1230,7 +1228,7 @@ function metatag_get_info($type = NULL, $name = NULL) { if (!isset($info)) { // hook_metatag_info() includes translated strings, so each language is cached // separately. - $cid = 'info:' . $GLOBALS['language']->language; + $cid = 'info:' . LANGUAGE_NONE; if ($cache = cache_get($cid, 'cache_metatag')) { $info = $cache->data; @@ -1505,6 +1503,48 @@ function metatag_config_access($op, $config = NULL) { } /** + * Identify the language identifier to be used with an entity. + * + * @param $entity_type + * An entity type's machine name. + * @param $entity + * The entity to review; + * @param $save_in_progress + * When an entity is being saved the results of entity_language() may be + * incorrect. + * + * @return + * A string indicating the language code to be used. + */ +function metatag_entity_get_language($entity_type, $entity) { + // If the entity doesn't support display translations just default to + // LANGUAGE_NONE. + $types = entity_get_info(); + if (empty($types[$entity_type]['translation']['locale'])) { + return LANGUAGE_NONE; + } + + // Determine the entity language as per http://drupal.org/node/1626346. + // Ignore this during entity updating + if (function_exists('entity_language')) { + $entity_language = entity_language($entity_type, $entity); + } + + // Failover to the older method of entity language definition. + if (empty($entity_language)) { + if (isset($entity->language)) { + $entity_language = $entity->language; + } + // Nothing available. + else { + $entity_language = LANGUAGE_NONE; + } + } + + return $entity_language; +} + +/** * Implements of hook_features_api(). */ function metatag_features_api() {