Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292926

field_language shouldn't cache display_languages for new entities

$
0
0

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.


Viewing all articles
Browse latest Browse all 292926

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>