diff --git a/tests/title.test b/tests/title.test index 21d2139..07ce44f 100644 --- a/tests/title.test +++ b/tests/title.test @@ -304,6 +304,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { 'changed' => REQUEST_TIME, ); entity_translation_get_handler('taxonomy_term', $term)->setTranslation($translation); + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($translation_langcode); taxonomy_term_save($term); $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.'); $term = $this->termLoad($term->tid, $translation_langcode); @@ -312,6 +313,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { // Delete the translation. entity_translation_get_handler('taxonomy_term', $term)->removeTranslation($translation_langcode); + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($translation_langcode); taxonomy_term_save($term); $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.'); $term = $this->termLoad($term->tid, $langcode); @@ -319,6 +321,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { // Make the term language neutral. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage(LANGUAGE_NONE); + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($translation_langcode); foreach ($original_values as $name => $value) { $field_name = $name . '_field'; $term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode]; @@ -330,6 +333,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { $this->assertTrue($this->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.'); // Change the term language to the former translation language. + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($translation_langcode); entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($translation_langcode); foreach ($original_values as $name => $value) { $field_name = $name . '_field'; @@ -358,6 +362,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { * Tests taxonomy form translation workflow. */ public function testFormTranslationWorkflow() { + $languages = language_list(); // Create a taxonomy term and check that legacy fields are properly // populated. $original_values = array( @@ -378,7 +383,9 @@ class TitleTranslationTestCase extends DrupalWebTestCase { ); $translation_langcode = 'it'; $edit = $this->editValues($translated_values, 'it'); - $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save')); + $this->drupalPost('taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'), array( + 'language' => $languages[$translation_langcode], + )); $term = $this->termLoad($term->tid); $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.')); $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.')); @@ -387,6 +394,12 @@ class TitleTranslationTestCase extends DrupalWebTestCase { $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.')); $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.')); + // Check that legacy fields are properly synchronized on entity load. + $term = $this->termLoad($term->tid, $translation_langcode); + foreach ($translated_values as $name => $value) { + $this->assertEqual($term->{$name}, $value, t('Legacy field "@field" is properly synchronized when term is loaded in translation language.', array('@field' => $name))); + } + // Updated the taxonomy term translation and check that both the original // values and the translations were correctly stored. $translated_values = array( @@ -394,7 +407,12 @@ class TitleTranslationTestCase extends DrupalWebTestCase { 'description' => $this->randomName(), ); $edit = $this->editValues($translated_values, $translation_langcode); - $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save')); + // Additionally, we test that everything works even if interface/content + // language is different from the translation language, for example, at + // en/taxonomy/term/123/edit/it path. + $this->drupalPost('taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'), array( + 'language' => $languages[$langcode], + )); $term = $this->termLoad($term->tid); $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.')); $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.')); @@ -409,8 +427,24 @@ class TitleTranslationTestCase extends DrupalWebTestCase { */ protected function termLoad($tid, $langcode = NULL) { drupal_static_reset(); - title_active_language($langcode); - return current(entity_load('taxonomy_term', array($tid), array(), TRUE)); + // If langcode is passed, we temporary replace global languages with the + // desired one. + $original_languages = array(); + if ($langcode !== NULL) { + $languages = language_list(); + foreach (drupal_language_types() as $language_type => $_) { + $original_languages[$language_type] = $GLOBALS[$language_type]; + $GLOBALS[$language_type] = $languages[$langcode]; + } + } + $term = current(entity_load('taxonomy_term', array($tid), array(), TRUE)); + // Restore original languages. + if (!empty($original_languages)) { + foreach (drupal_language_types() as $language_type => $_) { + $GLOBALS[$language_type] = $original_languages[$language_type]; + } + } + return $term; } /** diff --git a/title.module b/title.module index ee53c42..13e0ed5 100644 --- a/title.module +++ b/title.module @@ -150,7 +150,7 @@ function title_entity_presave($entity, $entity_type) { // need to synchronize the legacy field values into the replacing field // translations in the active language. if (module_invoke('entity_translation', 'enabled', $entity_type)) { - $langcode = title_active_language(); + $langcode = title_active_language($entity_type, $entity); $translations = entity_translation_get_handler($entity_type, $entity)->getTranslations(); // If we are removing a translation for the active language we need to skip // reverse synchronization, as we would store empty values in the original @@ -425,7 +425,7 @@ function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALS list($id, , $bundle) = entity_extract_ids($entity_type, $entity); if (!isset($langcode)) { - $langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language(); + $langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language($entity_type, $entity); } // We do not need to perform synchronization more than once. @@ -527,28 +527,35 @@ function title_field_sync_set($entity_type, $entity, $legacy_field, $info, $lang } /** - * Returns and optionally stores the active language. + * Returns the entity active language. * - * @param string $langcode - * (optional) The active language to be set. If none is provided the active - * language is just returned. + * The resulting value depends on the translation workflow: + * - if entity translation is used, it's form language or content language, + * - if content translation is used, it's current content language. + * + * @param string $entity_type + * The entity type. + * @param object $entity + * The entity object. * * @return string - * The active language code. Defaults to the current content language. - */ -function title_active_language($langcode = NULL) { - static $drupal_static_fast; - if (!isset($drupal_static_fast)) { - $drupal_static_fast['active_language'] = &drupal_static(__FUNCTION__); - } - $active_langcode = &$drupal_static_fast['active_language']; - if (isset($langcode)) { - $active_langcode = $langcode; - } - if (empty($active_langcode)) { - $active_langcode = $GLOBALS['language_content']->language; + * The language code. + */ +function title_active_language($entity_type, $entity) { + if ($handler = module_invoke('entity_translation', 'get_handler', $entity_type, $entity)) { + $form_language = $handler->getFormLanguage(); + if ($form_language == $handler->getLanguage() && $form_language != $GLOBALS['language_content']->language) { + // If form language is the same as entity original language, but differs + // from current content language, we assume that entity was loaded for the + // "view" purpose, so we return the content language. + // This is a bit ugly check, but there is no other way because + /* @see EntityTranslationDefaultHandler::getFormLanguage() */ + // automatically falls back to the entity "source" language. + return $GLOBALS['language_content']->language; + } + return $form_language; } - return $active_langcode; + return $GLOBALS['language_content']->language; } /**