EntityViewDisplay::buildMultiple enforces the langcode even when viewing the default translation

Created on 4 October 2023, about 1 year ago
Updated 29 March 2024, 9 months ago

Coming from 🐛 Inconsistent paragraph translations containing untranslated entity_reference fields Active .
A workaround suggested by berdir was to subclass the default entity reference formatter and override getEntitiesToView() to use the current language from language manager instead of $langcode.

But it looks like $langcode initially comes from EntityViewDisplay::buildMultiple which forces it to the current entity language as follows:

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

Which later defines/forces the langcode used on fields, including untranslated entity_reference fields, which can contain references to translated content.
I wonder, wouldn't it make more sense to add && !$entity->isDefaultTranslation() to that condition so that we don't enforce the langcode if the entity translation is the default one?

💬 Support request
Status

Active

Version

11.0 🔥

Component
Entity 

Last updated about 16 hours ago

Created by

🇧🇪Belgium herved

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @herved
  • 🇧🇪Belgium herved

    Test to see what the CI says...

  • last update about 1 year ago
    29,653 pass
  • 🇧🇪Belgium herved

    This can be replicated with nodes (e.g.: basic pages) by simply adding an untranslated node reference field, adjusting the display to show the rendered entity, and using the following hierarchy and translations of basic pages:
    - EN level 0 (translated to FR)
    -- EN level 1 (untranslated)
    --- EN level 2 (translated to FR)

    The only difference with paragraphs is that translations of level 1 are created automatically upon saving the level 0 translations, which we don't get with nodes. But the end result is basically the same.
    When viewing FR level 0 with the above setup:
    - without the patch: we see EN Level 1, EN Level 2
    - with the patch: we see EN Level 1, FR Level 2

    I'll try to create a phpunit test when I have some time so it makes more sense.

  • 🇵🇹Portugal jolimas

    I've tested this and it works.

    I think your approach to the problem is correct.

  • 🇫🇷France Adsyy

    Agree and tested too, works perfectly !

Production build 0.71.5 2024