You are here

function hook_field_widget_multivalue_WIDGET_TYPE_form_alter in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/field.api.php \hook_field_widget_multivalue_WIDGET_TYPE_form_alter()

Alter multi-value widget forms for a widget provided by another module.

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

To alter the individual elements within the widget, loop over \Drupal\Core\Render\Element::children($elements).

Parameters

array $elements: The field widget form elements as constructed by \Drupal\Core\Field\WidgetBase::formMultipleElements().

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

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

See also

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

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

hook_field_widget_multivalue_form_alter()

Related topics

4 string references to 'hook_field_widget_multivalue_WIDGET_TYPE_form_alter'
field_test_field_widget_multivalue_test_field_widget_multiple_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
field_test_field_widget_multivalue_test_field_widget_multiple_single_value_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
FormTest::testFieldFormMultipleWidgetTypeAlter in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
FormTest::testFieldFormMultipleWidgetTypeAlterSingleValues in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter() with single value elements.
2 functions implement hook_field_widget_multivalue_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_multivalue_test_field_widget_multiple_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
field_test_field_widget_multivalue_test_field_widget_multiple_single_value_form_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().

File

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

Code

function hook_field_widget_multivalue_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {

  // Code here will only act on widgets of type WIDGET_TYPE. For example,
  // hook_field_widget_multivalue_mymodule_autocomplete_form_alter() will only
  // act on widgets of type 'mymodule_autocomplete'.
  // Change the autocomplete route for each autocomplete element within the
  // multivalue widget.
  foreach (Element::children($elements) as $delta => $element) {
    $elements[$delta]['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
  }
}