By gábor hojtsy on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
In Drupal 7 and before, the 'language' property on nodes ($node->language) signified the language the node is saved with. As part of a bigger unification effort to differentiate language codes from language objects in the API, this was renamed to 'langcode' ($node->langcode). Now if you see 'language', that should be a language object and not a language code.
Along with that change, two minor things were also changed for consistency:
- The 'language' form element in the node edit form was renamed to 'langcode'.
- The 'language' variable that is used for theming nodes (
'#theme' => 'node') was renamed to 'langcode'. See the examples below for more information.
Drupal 7:
// Returns the language code of a node.
$node = node_load(1);
return $node->language;
// Unsets the language element on the node form.
function example_form_node_form_alter(&$form, &$form_state) {
unset($form['language']);
}
// Returns the language code of a node as a renderable array.
$render_array = node_view($node);
return $render_array['#language'];
// Preprocesses nodes for theming. Sets a custom language code.
function example_preprocess_node(&$variables) {
$variables['language'] = 'en-gb';
}
Drupal 8:
// Returns the language code of a node.
$node = node_load(1);
return $node->get('langcode')->value;
// Unsets the language element on the node form.
function example_form_node_form_alter(&$form, &$form_state) {
unset($form['langcode']);
}
// Returns the language code of a node as a renderable array.
$render_array = node_view($node);
return $render_array['#langcode'];
// Preprocesses nodes for theming. Sets a custom language code.
function example_preprocess_node(&$variables) {
$variables['langcode'] = 'en-gb';
}
Impacts:
Module developers
Themers
Comments
Drupal 8 code is not correct.
Drupal 8 code is not correct. Please correct the same.
fixed
fixed way to get langcode