The field_language() function may cause issues when used on node revisions.
To reproduce:
- Install a clean Drupal 7 installation ("Standard" profile for fast testing purposes)
- Enable the "Locale" & "Content translation" modules
- Make sure the body field is translatable (AFAIK - currently the only way to achieve is to manually change the "translatable" value to "1" in the "field_config" table for the "body" field)
- Create a new node, of type "Article". Enter a title, but leave the body field empty.
- Create a new revision of the node you just created, now enter some content in the body field. Visit the node view. The body field is displayed.
- Implement hook_init() in a custom module. Load the first revision of the node you just created & retrieve the languages for it's fields, eg.
/** * Implements hook_init(). */ function issue_node_revision_field_language_init() { $nid = 1; $vid = 1; $node = node_load($nid, $vid); if ($node) { $language = field_language('node', $node); } }
- Enable the custom module you just created.
- Go back to the node you created. You will see the body content you entered, is not displayed anymore. If you go to the edit form, there is content in it however.
Problem
The root of the problem lies within the field_language() function: https://api.drupal.org/api/drupal/modules%21field%21field.multilingual.i... .
If a field is not set on the entity, the language code of that particular field is set to LANGUAGE_NONE.
In the case of nodes, if working with revisions - there's the possibility that one revision has certain fields not set, while another revision may have it set. In my previous example the first revision doesn't have the body field set, thus get's the language code LANGUAGE_NONE. The second revision however, does have the body field set - and due to the field being translatable, the language code is 'en'.
field_language() puts it's processed results in the static cache, keyed by entity_type/entity_id/entity_language. Because there's no distinction between node revisions, field_language() falsely believes the language code of the body field is LANGUAGE_NONE.
Approaches
Unsure. My first thoughts are manually clearing the &drupal_static('field_language', array()) contents for that particular node, upon displaying a revision?
Comments
Comment #0.0
rp7 CreditAttribution: rp7 commentedDouble title.
Comment #1
rp7 CreditAttribution: rp7 commentedFYI: I discovered this issue using the Workbench Moderation module.
How I currently solved it:
Comment #1.0
rp7 CreditAttribution: rp7 commentedOn display; instead of loading.
Comment #2
recrit CreditAttribution: recrit commentedSee #2201251: field_language shouldn't cache display_languages for new entities for a patch that caches by vid and ignores new entities.