+/**
+ * Alter forms for multi-value field widgets provided by other modules.
+ *
+ * To alter the individual elements within the widget, loop over
+ * \Drupal\Core\Render\Element::children($elements).
+ *
+ * @param array $elements
+ * The field widget form elements as constructed by
+ * \Drupal\Core\Field\WidgetBase::formMultipleElements().
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The current state of the form.
+ * @param array $context
+ * An associative array containing the following key-value pairs:
+ * - 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.
+ * - widget: The widget plugin instance.
+ * - items: The field values, as a
+ * \Drupal\Core\Field\FieldItemListInterface object.
+ * - default: A boolean indicating whether the form is being shown as a dummy
+ * form to set default values.
+ *
+ * @see \Drupal\Core\Field\WidgetBaseInterface::form()
+ * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
+ * @see hook_field_widget_multivalue_WIDGET_TYPE_form_alter()
+ */
+function hook_field_widget_multivalue_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
+ // Add a css class to widget form elements for all fields of type mytype.
+ $field_definition = $context['items']->getFieldDefinition();
+ if ($field_definition->getType() == 'mytype') {
+ // Be sure not to overwrite existing attributes.
+ $elements['#attributes']['class'][] = 'myclass';
+ }
+}
+
+/**
+ * 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).
+ *
+ * @param array $elements
+ * The field widget form elements as constructed by
+ * \Drupal\Core\Field\WidgetBase::formMultipleElements().
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The current state of the form.
+ * @param array $context
+ * An associative array. See hook_field_widget_multivalue_form_alter() for
+ * the structure and content of the array.
+ *
+ * @see \Drupal\Core\Field\WidgetBaseInterface::form()
+ * @see \Drupal\Core\Field\WidgetBase::formMultipleElements()
+ * @see hook_field_widget_multivalue_form_alter()
+ */
+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';
+ }
+}
+