diff --git a/metatag.module b/metatag.module index c24e291..5ddefab 100644 --- a/metatag.module +++ b/metatag.module @@ -522,8 +522,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,10 +535,24 @@ function metatag_entity_update($entity, $entity_type) { list($id) = entity_extract_ids($entity_type, $entity); if (isset($entity->metatags)) { - // 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); + + // Identify the entity's old language value, delete the old record if that + // value has changed. + $metatags = metatag_metatags_load_multiple($entity_type, array($id)); + if (!empty($metatags[$id])) { + $metatags = $metatags[$id]; + $languages = array_keys($metatags); + $old_language = array_pop($languages); + if ($old_language != $language) { + db_delete('metatag') + ->condition('entity_type', $entity_type) + ->condition('entity_id', $id) + ->condition('language', $old_language) + ->execute(); + } + } metatag_metatags_save($entity_type, $id, $entity->metatags, $language); } @@ -624,9 +637,15 @@ function metatag_entity_view($entity, $entity_type, $view_mode, $langcode) { 'view_mode' => $view_mode, ); + // Identify the language to use with this entity. + $entity_language = metatag_entity_get_language($entity_type, $entity); + // Ensure we actually pass a language object rather than language code. $languages = language_list(); - if (isset($langcode) && isset($languages[$langcode])) { + if (isset($languages[$entity_language])) { + $options['language'] = $languages[$entity_language]; + } + elseif (isset($languages[$langcode])) { $options['language'] = $languages[$langcode]; } @@ -672,24 +691,13 @@ 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; + // 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]; } + // We weren't given a language; use the global content one. 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]; + $metatags = $metatags[$GLOBALS['language_content']->language]; } elseif (!empty($metatags[LANGUAGE_NONE])) { $metatags = $metatags[LANGUAGE_NONE]; @@ -1147,20 +1155,12 @@ 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]; @@ -1178,9 +1178,14 @@ function metatag_field_attach_form($entity_type, $entity, &$form, &$form_state, // 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 (!empty($entity->metatags[$entity_language])) { $metatags = $entity->metatags[$entity_language]; } + // 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(); @@ -1505,6 +1510,36 @@ 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; + * + * @return + * A string indicating the language code to be used. + */ +function metatag_entity_get_language($entity_type, $entity) { + // Determine the entity language as per http://drupal.org/node/1626346. + 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() {