You are here

function hook_field_widget_WIDGET_TYPE_form_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/field.api.php \hook_field_widget_WIDGET_TYPE_form_alter()
  2. 7 modules/field/field.api.php \hook_field_widget_WIDGET_TYPE_form_alter()

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

Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a specific widget form, rather than using hook_field_widget_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_multivalue_WIDGET_TYPE_form_alter() instead and loop over the elements.

Parameters

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

$form_state: The current state of the form.

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

Deprecated

in drupal:9.2.0 and is removed from drupal:10.0.0. Use hook_field_widget_single_element_WIDGET_TYPE_form_alter instead.

See also

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

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

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

hook_field_widget_form_alter()

hook_field_widget_multivalue_WIDGET_TYPE_form_alter()

Related topics

7 functions implement hook_field_widget_WIDGET_TYPE_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_complete_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_complete_form_alter().
field_test_field_widget_complete_test_field_widget_multiple_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
field_test_field_widget_complete_test_field_widget_multiple_single_value_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
field_test_field_widget_single_element_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_single_element_form_alter().
media_field_widget_complete_form_alter in core/modules/media/media.module
Implements hook_field_widget_complete_form_alter().

... See full list

File

core/modules/field/field.api.php, line 235
Field API documentation.

Code

function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {

  // Code here will only act on widgets of type WIDGET_TYPE.  For example,
  // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  // widgets of type 'mymodule_autocomplete'.
  $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
}