Remove cached languages from ContentEntityBase

Created on 15 July 2014, almost 10 years ago
Updated 30 January 2023, over 1 year 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 1 day 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.69.0 2024