Document that NULL is an allowed return value for EntityRepositoryInterface::getTranslationFromContext()

Created on 28 August 2023, over 1 year ago
Updated 12 November 2023, about 1 year ago

Problem/Motivation

In the interface EntityRepositoryInterface the function getTranslationFromContext() should always return EntityInterface.

And in Core's implementation, it always returns the default translation of the entity, if no other matches are found.
Here is a bug about this problem πŸ› EntityRepository::getTranslationFromContext() function forcibly adds default entity language to fallback candidates Needs review that will be resolved via a workaround, not via the real fix.

So, returning an entity is not always possible, and my suggestion is to allow returning NULL also.

If we have a custom logic, that disables access to some languages or translations, and the entity default language matches the disabled language, the only way is to return NULL, to indicate that the translation to the desired language is not allowed.

Steps to reproduce

1. Configure three languages: French (fr), Dutch (nl), German (de).

2. Configure a fallback chain from Dutch (nl) to have a transition to only German (de), so make French (fr) as disabled fallback for it.

3. Create a Node in French (fr) language.

4. Require a fallback of this Node in Dutch (nl) language.

You will receive a translation in French (fr), which is disabled as a fallback for the Dutch (nl) language!

Proposed resolution

The solution of the described problem is to return NULL for such cases. And the first step is to allow NULL in the interface. The next step is to modify the EntityRepository::getTranslationFromContext() to return NULL instead of the default translation.

Remaining tasks

Review the proposal of extending the interface, and move forward.
When πŸ› EntityRepository::getTranslationFromContext() function forcibly adds default entity language to fallback candidates Needs review is merged, create a new merge request to return NULL by default, without the flag 'strict_fallback'.

User interface changes

None

API changes

The EntityRepositoryInterface::getTranslationFromContext() function will be allowed to return NULL value.

Change record: https://www.drupal.org/node/3383698 β†’

Data model changes

Release notes snippet

The EntityRepositoryInterface::getTranslationFromContext() function is extended to allow returning NULL in cases when the requested translation is missing, forbidden, or unavailable.

πŸ“Œ Task
Status

Fixed

Version

10.1 ✨

Component
Language systemΒ  β†’

Last updated about 23 hours ago

  • Maintained by
  • πŸ‡©πŸ‡ͺGermany @sun
Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

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

Comments & Activities

Production build 0.71.5 2024