function hook_field_widget_form_alter

Same name and namespace in other branches
  1. 9 core/modules/field/field.api.php \hook_field_widget_form_alter()
  2. 8.9.x core/modules/field/field.api.php \hook_field_widget_form_alter()

Alter forms for field widgets provided by other modules.


$element: The field widget form element as constructed by hook_field_widget_form().

$form_state: An associative array containing the current state of the form.

$context: An associative array containing the following key-value pairs, matching the arguments received by hook_field_widget_form():

  • form: The form structure to which widgets are being attached. This may be a full form structure, or a sub-element of a larger form.
  • field: The field structure.
  • instance: The field instance structure.
  • langcode: The language associated with $items.
  • items: Array of default values for this field.
  • delta: The order of this item in the array of subelements (0, 1, 2, etc).

See also



Related topics

1 function implements hook_field_widget_form_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

field_test_field_widget_form_alter in modules/field/tests/field_test.module
Implements hook_field_widget_form_alter().


modules/field/field.api.php, line 922


function hook_field_widget_form_alter(&$element, &$form_state, $context) {
    // Add a css class to widget form elements for all fields of type mytype.
    if ($context['field']['type'] == 'mytype') {
        // Be sure not to overwrite existing attributes.
        $element['#attributes']['class'][] = 'myclass';

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.