getTranslatedFieldFromEntity should use the content language

Created on 28 September 2020, about 4 years ago
Updated 28 June 2023, over 1 year ago

Problem/Motivation

At the moment, the Block fetches the translated field from the entity using the getCurrentLanguage() function. The problem being that the default for this is the interface language. If you've defined a separate language for content, it won't work.

My use case being that the sites interface is always in English, but the content can be in many different languages.

Proposed resolution

There are two options I can see.

  • Always use the content language. This feels safe, the block is content after all.
  • Provide an option in the block management screen that asks if you want the content language or interface language.

I've provided a patch for option 1. It also adds the language to the cache contexts. This needs to be done regardless.

Remaining tasks

Decide on the approach to take
Commit.

User interface changes

Depends on approach

API changes

Depends on approach

Data model changes

Depends on approach

🐛 Bug report
Status

Active

Version

2.2

Component

Code

Created by

🇳🇿New Zealand danielveza Brisbane, AU

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.

  • First commit to issue fork.
  • @stewest opened merge request.
  • Status changed to Active over 1 year ago
  • 🇳🇿New Zealand stewest Wellington

    I'd like to open this up again as I can reproduce the issue.

    We're using Drupal 9.5.5 with the Sector Distribution.

    When I add a field as a block, like the subtitle or banner image field (all with translations enabled) and using Content language (instead of interface language), when I add a translation, I expect to see the translated content on the page, but I see the original languages content.

    The patch at https://www.drupal.org/project/fieldblock/issues/3173729#comment-13840638 🐛 getTranslatedFieldFromEntity should use the content language Active works for me.

Production build 0.71.5 2024