Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Am doing some testing trying to track down a Metatag bug report (#1845326: Metatags not loading correctly with beta4) and I've ran into an interesting scenario.
- Install the site with the default language of English.
- Enable the Locale module.
- Add a new language, e.g. Spanish, but leave the current default as English.
- Enable translations for e.g. the Article content type.
- Create an Article node and assign it the language Spanish.
- When the node is displayed:
- node_page_view($node) is passed the node object.
- node_show() is passed the node object.
- node_view_multiple() is passed the node object, the $view_mode of 'full', but the $weight and $langcode values are not passed thus adopt the default values of 0 and NULL respectively.
- node_view() is passed the node object, the $view_mode of 'full', and $langcode set to NULL.
- In node_view() it identifies that the $langcode is NULL and uses the system default language, i.e. English.
- When node_build_content() is loaded the system has already decided to use the global default language of English.
- When the various entity and node view hooks are invoked they are passed the system default language instead of the entity's language value.
So, the question is: when contrib modules are interacting with a page display via hook_entity_view, should they use the entity's language (via entity_language()) or should they use the $langcode value that's passed via the hook?
Thanks.
Comments
Comment #1
DamienMcKennaFurther compounding this is that when a Spanish node on a English site with a default language of English is edited, the form hooks are all passed the $langcode for Spanish, not the English site default.
To me this suggests that a) when editing an entity you should use the $langcode value supplied, b) when viewing an entity you should first check for matching data against the $langcode value supposed, then default to the entity's language code if nothing found.
So when should LANGUAGE_NONE be used?
Comment #2
DamienMcKennaAnother problem, is there a standard practice on what to do when updating an existing object if the language value is changed? Is it best practice to manually identify the old language value and delete old records before saving new data, should it be left around, or should the action be determined by e.g. whether entity_translation is also enabled?