Problem/Motivation

This is related to #2543258: Entity references of untranslatable fields are not displayed in the correct translation. It's correct that entity references of untranslatable fields should get displayed in the correct translation according to the user language on translatable entities.

When viewing untranstatable entities though, i would expect to see the translated references according to the global language.

How to re-produce this:

  1. Enable the content_translation
  2. Add a new language to the site
  3. Make the tags vocabulary translatable
  4. Add one or 2 terms and translate them
  5. Make sure the node type article is not translatable (default)
  6. Create a node and refer the translated terms
  7. Access the node in both languages and see that the tags are always rendered in the default language

Proposed resolution

Check whether the containing entity type/ bundle is translatable and display the reference's translations according to the global language.

Comments

s_leu created an issue. See original summary.

s_leu’s picture

Status: Active » Needs review
StatusFileSize
new1.5 KB

Here's first patch that determines whether the entity's type is translatable and sets the corresponding argument for $formatter->view().

Status: Needs review » Needs work

The last submitted patch, 2: entity-reference-display-translations-2584745-1.patch, failed testing.

s_leu’s picture

New patch after discussing this with Berdir.

berdir’s picture

Issue tags: +Needs tests
gábor hojtsy’s picture

Issue tags: +D8MI, +language-content, +sprint
yched’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php
    @@ -249,7 +250,13 @@ public function buildMultiple(array $entities) {
    +          // Display fields for untranslatable entities in the current content
    +          // language.
    

    Yeah, actually this is not the language for the field values (since the entity is not translatable, the values don't depend on language), but the language that the formatter should use for "what it does with the field values".

    This area is a bit confusing, and we've been adjusting it back and forth a couple times, so IMO this deserves some explicit comment here.

    Here's what I can come up with atm:
    "The language of the field values to display is already determined in the incoming $entity. The formatter should build its output of those values using:
    - the entity language if the entity is translatable,
    - the current "content language" otherwise."

    Suggestions / refinements welcome :-)

  2. +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php
    @@ -249,7 +250,13 @@ public function buildMultiple(array $entities) {
    +          $view_langcode = $entity->language()->getId();
    +          if ($entity instanceof TranslatableInterface && !$entity->isTranslatable()) {
    +            $view_langcode = NULL;
    +          }
    

    Nitpick, but I'd find a ternary, or if too long an explicit if/else, to be clearer.

    Also, what if *not* instanceof TranslatableInterface ? we keep the entity langcode as if the entity was translatable ? seems weird ?
    Shouldn't it be :

    if ($entity instanceof Translatable && $entity->isTranslatable()) {
      $view_langcode = $entity->language()->getId(); 
    }
    else {
      $view_langcode = NULL;  
    }
    

    ?

plach’s picture

Also, what if *not* instanceof TranslatableInterface ? we keep the entity langcode as if the entity was translatable ? seems weird ?

Aside from the fact that this should not be very common, I think it would probably make sense not provide a language also in this case. The entity language can be retrieved from the items if needed.

yched’s picture

@plach : meaning, you're +1 on the proposal at the end of #7 ? (just making sure ;-)

mbovan’s picture

Addressed the feedback from #7. Tests still needed.

plach’s picture

Yep, that was a verbose +1 :)

mbovan’s picture

The last submitted patch, 12: entity_references-2584745-12-TEST-ONLY.patch, failed testing.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

I also encountered this problem while working on #2144377: Entity reference autocomplete lists entity labels only in current content language, and the fix looks good, as agreed upon in #9/#11.

berdir’s picture

Issue tags: -Needs tests +rc target triage

Great to see this RTBC.

Tagging with rc target triage. It's not a huge problem, but we did run into both this and #2144377: Entity reference autocomplete lists entity labels only in current content language in our very first multilingual D8 project and I think many other sites will too if you e.g. use term references on users.

It's a small change in the behavior, but I don't think anyone expects the current behavior and we have agreement from @yched, @plach, @amateescu and @GaborHoitsy also agreed in IRC.

The last submitted patch, 2: entity-reference-display-translations-2584745-1.patch, failed testing.

The last submitted patch, 12: entity_references-2584745-12-TEST-ONLY.patch, failed testing.

xjm’s picture

Issue tags: -rc target triage +rc target

Discussed with the D8 committers and we agreed with making this an rc target.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x, thanks!

  • catch committed 4a11bb5 on 8.0.x
    Issue #2584745 by mbovan, s_leu: Entity references should be displayed...
gábor hojtsy’s picture

Issue tags: -sprint

Yay, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.