diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 69741a123e..44331500d8 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -262,8 +262,12 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\F * @see hook_field_widget_multiple_WIDGET_TYPE_form_alter() */ function hook_field_widget_multiple_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) { - // Add a css class to the overall element of any widget. - $elements['#attributes']['class'][] = 'myclass'; + // 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'; + } } /** @@ -290,18 +294,13 @@ function hook_field_widget_multiple_form_alter(array &$elements, \Drupal\Core\Fo * @see hook_field_widget_multiple_form_alter() */ function hook_field_widget_multiple_WIDGET_TYPE_form_alter(array &$elements, \Drupal\Core\Form\FormStateInterface $form_state, array $context) { - // Alter the overall title for the multivalue widget (the table header, - // above the rows). - $elements['#title'] = t('Review your %title entries', ['%title' => $elements['#title']]); - - if (!empty($elements['#description'])) { - // Add a paragraph to the description, above all the rows. - $elements['#description'] = [ - '#markup' => $elements['#description'], - ]; - } - else { - $elements['#description'] = t('These items might be out of date.'); + // 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'. + // Change the autcomplete route for each autocomplete element within the + // multivalue widget. + foreach (Element::children($elements) as $delta => $element) { + $elements[$delta]['#autocomplete_route_name'] = 'mymodule.autocomplete_route'; } } diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index 9121e942ea..15f8708adf 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -101,15 +101,6 @@ function field_test_entity_display_build_alter(&$output, $context) { */ function field_test_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) { $field_definition = $context['items']->getFieldDefinition(); - switch ($field_definition->getName()) { - case 'alter_test_text': - drupal_set_message('Field size: ' . $context['widget']->getSetting('size')); - break; - - case 'alter_test_options': - drupal_set_message('Widget type: ' . $context['widget']->getPluginId()); - break; - } // Set a message if this is for the form displayed to set default value for // the field. if ($context['default']) {