Optimize ContentEntityBase::getTranslatedField and do not store field values under the default and the asked langcode under $values

Created on 16 November 2016, about 8 years ago
Updated 24 January 2023, almost 2 years ago

Problem/Motivation

I've noticed that ContentEntityBase::getTranslatedField is called recursively for non translatable fields and also in __sleep the field values of non translatable fields will be stored twice and the function getValue will be called for all the translations for which a field has been initialized. Also if you iterate over $values you will get unneeded iterations over the same values. In the case where the an referenced entity of a non translatable field will be returned by FieldItemList::getValue this would cause $values to contain the referenced entity not only once but one time for the default langcode and additionally so many times as the number of entity translations, which in case of serializing the parent would cause a bigger serialized object.

Proposed resolution

Remove the recursive call of ContentEntityBase::getTranslatedField and store directly non translatable fields only under the default langcode by keeping track which field are translatable and which not.

Remaining tasks

Reroll
Tests (maybe)
Review.

User interface changes

API changes

Data model changes

📌 Task
Status

Needs review

Version

10.1

Component
Entity 

Last updated about 16 hours ago

Created by

🇩🇪Germany hchonov 🇪🇺🇩🇪🇧🇬

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.

  • 🇺🇸United States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

    Updated the IS remaining tasks field
    Reading the comments, specifically #4, this seems like it could be a bug and that we will need a test case showing the issue.

  • 🇮🇳India Abhisheksingh27

    Adding reroll for 10.1.x

  • Status changed to Needs work 2 months ago
  • 🇳🇱Netherlands roderik Amsterdam,NL / Budapest,HU

    I stumbled across this issue while trying to debug something (and I cannot find another related issue). I have no opinion on the original issue description here, but just want to drop a note.

    This is some current behavior of a non-translatable field on a translated entity, for better or worse:

    $entity = get_some_entity()
    // $entity->defaultLangcode: 'en'
    // $entity->activeLangcode : 'de'
    $field_item_list = $entity->get(NON_TRANSLATABLE_FIELD);
    $check_entity = $field_item_list->getEntity();
    // $check_entity->defaultLangcode: 'en'
    // $check_entity->activeLangcode : 'x-default'
    

    getEntity() gets the untranslated entity. I do not know if this was intended, or if this is documented anywhere...

    but from reading the patch, I believe that this will change to $check_entity->activeLangcode == 'de'.

    Maybe that's a good thing. But I'm guessing it needs to be taken into account, and documented somewhere.

    (And maybe there needs to be a test for this, going forward?...)

  • 🇳🇱Netherlands roderik Amsterdam,NL / Budapest,HU
Production build 0.71.5 2024