Remove cached languages from ContentEntityBase

Created on 15 July 2014, over 10 years ago
Updated 30 January 2023, almost 2 years ago


Please credit also @pfrenssen for his contribution to the duplicate issue #2907507: Static cache of languages in ContentEntityBase is not updated when a language is added .

Follow-up of #597236: Add entity caching to core

Problem/Motivation

The internal language cache can lead to weird cases where languages don't exist and need to be refreshed.

ContentEntityBase::getLanguages() statically caches the list of languages that it receives from the language manager. When later on a new language is added, this static cache is not cleared, and an outdated list is used. This prevents entities in the new language from being created.

Example use case: we are importing data using the Migrate module. At some point during the migration we encounter an entity that has a new language, so we create the new language. Saving the entity results in an exception:

error: Invalid translation language (cs) specified in ContentEntityBase.php:863

The sequence that triggers the error is:

  1. A new entity is instantiated.
  2. A command, such as ContentEntityBase::language(), is warming up the entity's static cache with the actual list of languages.
  3. A new language is added.
  4. A translation in the new language is added.

The exception is thrown because the statically cache list was not updated with the new language.

Proposed resolution

We should remove the ContentEntityBase::$languages from content entities and instead always get the language we want from the language manager.

However, for that, we should consider adding a langcode() method first (or getLanguageCode()), so that we can replace all cases of language()->getId() and minimize the calls to language() to those that actually need the object.

Remaining tasks

None.

User interface changes

None.

API changes

New method ContentEntityBase::getLanguageCode()

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Entity 

Last updated about 4 hours ago

Created by

🇨🇭Switzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
  • 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.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024