Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

In Drupal 7, widget input elements for Field API fields in entity $form structures appeared nested under a langcode key:

$form['body']['und']
$form['field_foo']['fr']
$form_state['values']['body']['und']
$form_state['values']['field_foo']['fr']

That langcode key corresponded to the language assigned to the field by some complex multilingual logic, making it hard for 3rd party code that attempted to manipulate the form (for example in a form_alter). To mitigate that, the $form['body'] element provided a '#language' property that contained the name of the langcode key, thus allowing external code to address the field widget with $form['body']['form']['body']['#language'].

In Drupal 8, that langcode nesting level is not needed anymore for correct processing of the submitted form, and has thus been removed. Instead, widgets for entity fields appear in entity $form structures at:

$form['body']['widget']
$form['field_foo']['widget']
$form_state['values']['body']
$form_state['values']['field_foo']

(For technical reasons, a separate sub-element is still needed in $form structures, it now has a constant, predictable key: 'widget'.)

Changes required

  • Code that interacts with the generated form structures in form_alter implementations needs to be updated for the new form structure.
  • Tests that use drupalPost() / drupalPostForm() to submit field values to entity forms no longer needs to include a langcode in the keys of the posted data.

See also: $langcode parameter removed in field_form_get_state() and field_form_set_state()

Code examples

Drupal 7:

<?php
/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 */
function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (isset(
$form['taxonomy_forums'])) {
   
$langcode = $form['taxonomy_forums']['#language'];
   
$widget =& $form['taxonomy_forums'][$form['taxonomy_forums']['#language']];
    if (empty(
$widget['#default_value'])) {
     
$widget['#default_value'] = 'my value';
    }
  }
}
?>

Drupal 8:

<?php
/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 */
function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (isset(
$form['taxonomy_forums'])) {
   
$widget =& $form['taxonomy_forums']['widget'];
    if (empty(
$widget['#default_value'])) {
     
$widget['#default_value'] = 'my value';
    }
  }
}
?>
Impacts: 
Module developers
Themers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done