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:
- A new entity is instantiated.
- A command, such as
ContentEntityBase::language()
, is warming up the entity's static cache with the actual list of languages.
- A new language is added.
- 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.