diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index b90d9b9..8fa66fd 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -240,7 +240,9 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f 'effect' => 'fade', ], ]; + } + if ($is_multiple) { // Allow modules to alter the field multiple widget form element. $context = [ 'form' => $form, @@ -254,6 +256,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f 'field_widget_multiple_' . $this->getPluginId() . '_form', ], $elements, $form_state, $context); } + } return $elements; } diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index aed3dde..0deabd4 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -228,12 +228,12 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\F * To adjust the individual widgets for the values themselves, use * hook_field_widget_form_alter() instead. * - * @param $element - * The field widget form element as constructed by - * \Drupal\Core\Field\WidgetBaseInterface::form(). - * @param $form_state + * @param array $elements + * The field widget form elements as constructed by + * \Drupal\Core\Field\WidgetBaseInterface::formMultipleElements(). + * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. - * @param $context + * @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. @@ -249,9 +249,9 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\F * @see hook_field_widget_form_alter() * @see hook_field_widget_multiple_WIDGET_TYPE_form_alter() */ -function hook_field_widget_multiple_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { +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. - $element['#attributes']['class'][] = 'myclass'; + $elements['#attributes']['class'][] = 'myclass'; } /** @@ -264,12 +264,12 @@ function hook_field_widget_multiple_form_alter(&$element, \Drupal\Core\Form\Form * To modify the widget for individual itesm, use * hook_field_widget_WIDGET_TYPE_form_alter() instead. * - * @param $element - * The field widget form element as constructed by - * \Drupal\Core\Field\WidgetBaseInterface::form(). - * @param $form_state + * @param array $elements + * The field widget form elements as constructed by + * \Drupal\Core\Field\WidgetBaseInterface::formMultipleElements(). + * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. - * @param $context + * @param array $context * An associative array. See hook_field_widget_form_alter() for the structure * and content of the array. * @@ -277,20 +277,19 @@ function hook_field_widget_multiple_form_alter(&$element, \Drupal\Core\Form\Form * @see \Drupal\Core\Field\WidgetBase::formSingleElement() * @see hook_field_widget_multiple_form_alter() */ -function hook_field_widget_multiple_WIDGET_TYPE_form_alter(&$elements, \Drupal\Core\Form\FormStateInterface $form_state, $context) { +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'] = \Drupal::t('Review your %title entries', ['%title' => $elements['#title']]); + $elements['#title'] = t('Review your %title entries', ['%title' => $elements['#title']]); - if (!empty($elements['#description'])) { + if (!empty($elements['#description'])) { // Add a paragraph to the description, above all the rows. $elements['#description'] = [ - '#markup' => \Drupal::t('These items might be out of date.'), '#markup' => $elements['#description'], ]; } else { - $elements['#description'] = \Drupal::t('These items might be out of date.'); + $elements['#description'] = t('These items might be out of date.'); } } diff --git a/core/modules/media/media.module b/core/modules/media/media.module index fa76bb4..4e98ed2 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -177,12 +177,13 @@ function media_form_field_ui_field_storage_add_form_alter(&$form, FormStateInter /** * Implements hook_field_widget_WIDGET_TYPE_form_alter(). */ -function media_field_widget_entity_reference_autocomplete_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { - // Only act on Media entity reference autocomplete fields. +function media_field_widget_entity_reference_autocomplete_form_alter(array &$element, FormStateInterface $form_state, array $context) { + // On autocomplete widgets, add some help text to aid users know what to do. + // Only act on entity reference fields that point to Media entities. if (!empty($element['target_id']['#type']) && ($element['target_id']['#type'] === 'entity_autocomplete') && ($element['target_id']['#target_type'] === 'media')) { $label = $element['target_id']['#title']; - $element['target_id']['#title'] = t('

Use existing %title media

', ['%title' => $label]); + $element['target_id']['#title'] = t('

Use existing %title

', ['%title' => $label]); $reuse_message = t('Type part of the media name. See the media list (opens a new window) to help locate media.', [':list' => Url::fromRoute('entity.media.collection')->toString()]); @@ -190,14 +191,14 @@ function media_field_widget_entity_reference_autocomplete_form_alter(&$element, $element['target_id']['#description'] = $reuse_message; } else { - $element['target_id']['#description'] = ['#markup' => $element['target_id']['#description'], '#markup' => $reuse_message]; + $element['target_id']['#description'] = ['#markup' => $element['target_id']['#description']]; } // @todo There must be an easier way to get this. $cardinality = $context['items']->getFieldDefinition()->getFieldStorageDefinition()->getCardinality(); - // Only add the suffix if the user also has permission to create - // media. Some sites might allow content authors to pick approved media - // but not to submit their own. + // Only add the suffix if the user also has permission to create media. Some + // sites might allow content authors to pick approved media but not to + // submit their own. // Don't add a suffix if the field is multi-value, as that would result in // repetitive, spammy headers on every item. if ($cardinality === 1) { @@ -215,33 +216,39 @@ function media_field_widget_entity_reference_autocomplete_form_alter(&$element, $access = \Drupal::entityTypeManager()->getAccessControlHandler('media')->createAccess(NULL, \Drupal::currentUser()); } if ($access) { - $new_message = t('

Uploading new %title media

Upload your media on the media upload form (opens a new window), then add it by name to the field below.', ['%title' => $label, ':add' => Url::fromRoute('entity.media.add_page')->toString()]); + $new_message = t('

Uploading new %title

Upload your media on the media upload form (opens a new window), then add it by name to the field below.', ['%title' => $label, ':add' => Url::fromRoute('entity.media.add_page')->toString()]); if (empty($element['#prefix'])) { $element['#prefix'] = $new_message; - } else { + } + else { $element['#prefix'] = Markup::create($element['#prefix'] . $new_message); } } + // Wrap the whole element in a new fieldset container. + if (!empty($element['#prefix'])) { + $element['#prefix'] = Markup::create('
' . $label . '' . $element['#prefix']); + $element['#suffix'] = !empty($element['#suffix']) ? Markup::create($element['#suffix'] . '
') : Markup::create(''); + } } } } - /** * Implements hook_field_widget_multiple_WIDGET_TYPE_form_alter(). */ -function media_field_widget_multiple_entity_reference_autocomplete_form_alter(&$elements, \Drupal\Core\Form\FormStateInterface $form_state, $context) { - // Only act on Media entity reference autocomplete fields. +function media_field_widget_multiple_entity_reference_autocomplete_form_alter(array &$elements, FormStateInterface $form_state, array $context) { + // On autocomplete widgets, add some help text to aid users know what to do. + // Only act on entity reference fields that point to Media entities. if (!empty($elements[0]['target_id']['#type']) && ($elements[0]['target_id']['#type'] === 'entity_autocomplete') && ($elements[0]['target_id']['#target_type'] === 'media')) { - // Use the user-defined label for the field to indicate that the form - // allows reusing existing media. + // Use the user-defined label for the field to indicate that the form allows + // reusing existing media. $label = $elements['#title']; - $elements['#title'] = t('

Use existing %title media

', ['%title' => $label]); + $elements['#title'] = t('

Use existing %title

', ['%title' => $label]); - // Only add the suffix if the user also has permission to create - // media. Some sites might allow content authors to pick approved media - // but not to submit their own. + // Only add the suffix if the user also has permission to create media. Some + // sites might allow content authors to pick approved media but not to + // submit their own. $access = FALSE; $settings = $context['items']->getSettings(); if (!empty($settings['handler_settings']['target_bundles'])) { @@ -256,7 +263,7 @@ function media_field_widget_multiple_entity_reference_autocomplete_form_alter(&$ $access = \Drupal::entityTypeManager()->getAccessControlHandler('media')->createAccess(NULL, \Drupal::currentUser()); } if ($access) { - $new_message = t('

Uploading new %title media

Upload your media on the media upload form (opens a new window), then add it by name to the field below.', ['%title' => $label, ':add' => Url::fromRoute('entity.media.add_page')->toString()]); + $new_message = t('

Uploading new %title

Upload your media on the media upload form (opens a new window), then add it by name to the field below.', ['%title' => $label, ':add' => Url::fromRoute('entity.media.add_page')->toString()]); if (empty($elements['#prefix'])) { $elements['#prefix'] = $new_message; } @@ -266,6 +273,9 @@ function media_field_widget_multiple_entity_reference_autocomplete_form_alter(&$ // clearfix floating things. $elements['#prefix'] = Markup::create($elements['#prefix'] . $new_message); } + // Wrap the whole element in a new fieldset container. + $elements['#prefix'] = Markup::create('
' . $label . '' . $elements['#prefix']); + $elements['#suffix'] = !empty($elements['#suffix']) ? Markup::create($elements['#suffix'] . '
') : Markup::create(''); } } }