The field_language function according to its documention: 'Returns the display language for the fields attached to the given entity.'

It caches its internal results per entity type and entity id (per individual entity):

list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!isset($display_languages[$entity_type][$id][$langcode])) { ... }

When you want to translate some node to some other language, Drupal will call field_attach_prepare_translation on a freshly created node object (without a nid because it is new).

The effect will be that $id = NULL, and thus an entry in $display_languages[$entity_type][''][$langcode] is created.

This feels bad because display_languages is supposed to cache per individual entity, while it is now using one cache entry for any new node translation.

When translating multiple nodes during the same call, a problem may arise because of this. In my situation, I had a piece of code that started off translating a node without a body set, and then subsequently tried translating a node with a body. Because the first node's body language was incorrectly set to LANGUAGE_NONE, the second one was mistakingly also set to LANGUAGE_NONE while in reality it should have been English.

Solution: do NOT use or fill the display_languages array when calling field_language on new nodes.

Comments

bvanmeurs’s picture

bvanmeurs’s picture

Title: field_language shouldn't cache display_languages for new nodes » field_language shouldn't cache display_languages for new entities
Issue tags: +drupal8
bvanmeurs’s picture

Status: Active » Needs review
recrit’s picture

Since there are many issues with field_language's static cache, I have added the related issues to this ticket.
The attached patch combines the following:
* (this issue) #2201251: field_language shouldn't cache display_languages for new entities - Fixes new entities cache collisions for same entity type and/or different bundles.
* #2069439: Static cache in field_language() should key by $vid as well as $id - Fixes same nid with different vid.

damienmckenna’s picture

I suspect it might be useful to have some tests that demonstrate the problem.

james.williams’s picture

Version: 7.26 » 7.x-dev
Status: Needs review » Needs work

On #1587874: field_language() caching to strict when creating multiple new entity types with different bundles, we used an approach that cached per-bundle, since new entities of the same bundle are expected to have the same set of fields & display languages. If there is a case for new entities of the same bundle to have a different set of display languages, I'd like to hear it. Otherwise, I think we should combine the caching between revisions (#2069439: Static cache in field_language() should key by $vid as well as $id) and bundle (#1587874: field_language() caching to strict when creating multiple new entity types with different bundles), even for new entities.

webservant316’s picture

My problem over here may be related...

https://www.drupal.org/project/course_admin/issues/3214041

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.