Wrong language handling in ImageFormatter when linking to entity

Created on 13 September 2016, over 8 years ago
Updated 20 March 2025, 29 days ago

Hello,

I am not sure whether this is a true bug as I am nowhere close to be a D8 savvy, but here's what I've got: when you're rendering an entity with image field attached to it and when the image formatter is configured to link to the entity, the resulting link does not account in which language it is being rendered and always links to the entity's original language. Alright, now translating this to English, here are some basic steps to reproduce:

  1. Get a vanilla fresh D8.1 release
  2. Enable all the 4 core translation modules
  3. Add a Spanish language to the website
  4. Enable content translation (particularly, we want to enable the translation of Article node type).
  5. Within Article node type only enable title and body translation (the important part is to keep "image" untranslatable)
  6. Go ahead and create an article node (let's make it original in English and then add translation of it in Spanish)
  7. Change the "default" display settings of article node: make sure "image" formatter is set up to link to its entity
  8. Navigate to a full page view of your article node in English. Observe the image to link to the English version of the node (expected result)
  9. Now navigate to a full page view of your article node in Spanish. Observe here the image to link to English version of the node (while the expected result is to have that link to the Spanish version).

(this bug disappears if we enable translation of the image field)

To me it seems more logical to link image to the translation language in which its entity is currently being rendered even if the image field is not translatable.

For some reason (I just can't debug whole drupal core to figure it out) in Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter::viewElements() the parent entity of the provided items list is set up to be in LANGCODE_DEFAULT instead of having explicitly set spanish language (since precisely in this language the entity is being rendered). What I currently do in image formatter method to overcome this issue is to force the parent entity to look up its translation in the language in which rendering is happening.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

entity system

Created by

🇺🇦Ukraine bucefal91

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

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.

  • 🇪🇸Spain javier_rey

    This has also happened to me in a Drupal 10.4.4.
    Tested it at https://simplytest.me/

    Steps to reproduce the error:

    1. Add a language to the web, in this case it will be English (default) and Spanish.
    2. In the language configuration, indicate that in English the prefix will be “en” and in Spanish it will be “es” (admin/config/regional/language/detection/url)
    3. Using the content type “basic page”, mark it as translatable and add an image media type field (not translatable)
    4. Mark as translatable also the media type “image”.
    5. Create 2 nodes in English, add an image to them and translate them into Spanish.

    Now comes the problem:

    1. Create a view that shows the content type “basic page”.
    2. Add the image field with the format “thumbnail (important, with the other formats it works fine)”.
    3. Check the “link to content” option in the image field.
    4. When checking the English view, both images point to the corresponding nodes in English:
      1. https://master-teaibnpl96czl1tjspp5qwvhrsw7quru.tugboatqa.com/en/node/1
      2. https://master-teaibnpl96czl1tjspp5qwvhrsw7quru.tugboatqa.com/en/node/2
    5. But if we switch to Spanish, the 2 images point to the English node.
      1. https://master-teaibnpl96czl1tjspp5qwvhrsw7quru.tugboatqa.com/en/node/1 (should be /es/node/1)
      2. https://master-teaibnpl96czl1tjspp5qwvhrsw7quru.tugboatqa.com/en/node/2 (should be /es/node/2)

      So the problem seems to be that when selecting the thumbnail formatter, it is not picking up the correct language of the content it has to link to.

  • First commit to issue fork.
  • 🇮🇳India KumudB Ahmedabad

    Improved Entity Translation Handling

    In Drupal 8.1, entity translation handling was still evolving. The language negotiation and rendering system had some inconsistencies in cases where an untranslatable field (image) was being used in translated content.
    In Drupal 11.x, the entity rendering system has improved, ensuring that links generated in untranslated fields respect the active page language.
    Changes in ImageFormatter::viewElements()

    The issue in Drupal 8.1 was that viewElements() used LANGCODE_DEFAULT, which caused links to always point to the original entity language.
    In Drupal 11.x, the rendering correctly respects the current entity translation.
    Field Translation System Fixes

    Earlier Drupal versions required making the image field translatable as a workaround.
    In Drupal 11.x, this is no longer required because entity rendering now properly resolves translation context for non-translatable fields.

Production build 0.71.5 2024