You are here

function hook_field_widget_multivalue_form_alter in Drupal 9

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

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).

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 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.

Deprecated

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

See also

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

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

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

hook_field_widget_multivalue_WIDGET_TYPE_form_alter()

Related topics

File

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

Code

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 my_type.
  $field_definition = $context['items']
    ->getFieldDefinition();
  if ($field_definition
    ->getType() == 'my_type') {

    // Be sure not to overwrite existing attributes.
    $elements['#attributes']['class'][] = 'my-class';
  }
}