Background
Calling node_preview() does not involve using the active language of the node at hand. Rather, through calling node_view() with no $langcode parameter set, it results in falling back to using the global content_language. For example, when a user clicks the 'Preview' button when editing a translation, in a system that does not have URL detection enabled, or when editing a translation of a node in a disabled language, the preview of the translated content comes in the content language, which is not always what is expected.

Workaround
Inside entity_translation_field_language_alter(), check whether the entity at hand is a node with the in_preview property set to TRUE. If this is the case, use the node's active language when calling locale_field_language_fallback().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stefanos.petrakis created an issue. See original summary.

stefanos.petrakis@gmail.com’s picture

And here is a patch (with tests).

plach’s picture

Status: Needs review » Needs work

Nice solution, almost good to go!

  1. +++ b/entity_translation.module
    @@ -938,6 +938,10 @@ function entity_translation_field_language_alter(&$display_language, $context) {
    +    elseif ($entity_type == 'node' && !empty($entity->in_preview)) {
    

    Bear with me, but I'd have one request: can we move this code to entity_translation.node.inc? I'm trying to keep all entity_type-specific code confined there (see for instance http://cgit.drupalcode.org/entity_translation/tree/entity_translation.mo...).

  2. +++ b/tests/entity_translation.test
    @@ -292,6 +292,49 @@ class EntityTranslationTranslationTestCase extends EntityTranslationTestCase {
    +   * Preview a node's translation.
    

    Previews ;)

  3. +++ b/tests/entity_translation.test
    @@ -292,6 +292,49 @@ class EntityTranslationTranslationTestCase extends EntityTranslationTestCase {
    +   * @param $node
    +   *   Node of the basic page to create translation for.
    +   * @param $title
    +   *   Title of the basic page in the specified language.
    +   * @param $body
    +   *   Body of the basic page in the specified language.
    +   * @param $langcode
    

    Missing @param types

  4. +++ b/tests/entity_translation.test
    @@ -292,6 +292,49 @@ class EntityTranslationTranslationTestCase extends EntityTranslationTestCase {
    +      $this->assertNotEqual(FALSE, $strpos_translation_body, t('Node preview uses the language of the form.'));
    ...
    +      $this->assertEqual(FALSE, $strpos_original_body, t('Node preview does not use the original language of the form.'));
    

    I'd use assertIdentical/assertNotIdentical here, to avoid unexpected failures should the body be moved to the very beginning of the string (very unlikely ;)

stefanos.petrakis@gmail.com’s picture

Status: Needs work » Needs review
FileSize
3.85 KB

Rerolling (against dev).

Regarding 1.: I pass two extra arguments to the entity-specific field language alteration functions, the $entity and the $handler, thought this would make for more economic code-writing.