function hook_field_widget_single_element_WIDGET_TYPE_form_alter

Same name in other branches
  1. 9 core/modules/field/field.api.php \hook_field_widget_single_element_WIDGET_TYPE_form_alter()
  2. 10 core/modules/field/field.api.php \hook_field_widget_single_element_WIDGET_TYPE_form_alter()

Alter widget forms for a specific widget provided by another module.

Modules can implement hook_field_widget_single_element_WIDGET_TYPE_form_alter() to modify a specific widget form, rather than using hook_field_widget_single_element_form_alter() and checking the widget type.

This hook can only modify individual elements within a field widget and cannot alter the top level (parent element) for multi-value fields. In most cases, you should use hook_field_widget_complete_WIDGET_TYPE_form_alter() instead and loop over the elements.

Parameters

array $element: The field widget form element as constructed by \Drupal\Core\Field\WidgetBaseInterface::form().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $context: An associative array. See hook_field_widget_single_element_form_alter() for the structure and content of the array.

See also

https://www.drupal.org/node/3180429

\Drupal\Core\Field\WidgetBaseInterface::form()

\Drupal\Core\Field\WidgetBase::formSingleElement()

hook_field_widget_single_element_form_alter()

hook_field_widget_complete_WIDGET_TYPE_form_alter()

Related topics

File

core/modules/field/field.api.php, line 260

Code

function hook_field_widget_single_element_WIDGET_TYPE_form_alter(array &$element, FormStateInterface $form_state, array $context) {
    // Code here will only act on widgets of type WIDGET_TYPE.  For example,
    // hook_field_widget_single_element_my_module_autocomplete_form_alter() will
    // only act on widgets of type 'my_module_autocomplete'.
    $element['#autocomplete_route_name'] = 'my_module.autocomplete_route';
}

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