diff --git a/core/modules/media_library/src/Form/AddFormBase.php b/core/modules/media_library/src/Form/AddFormBase.php index a7a79f9fd0..c3ec671198 100644 --- a/core/modules/media_library/src/Form/AddFormBase.php +++ b/core/modules/media_library/src/Form/AddFormBase.php @@ -165,6 +165,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Allow the current selection to be set in a hidden field so the selection // can be passed between different states of the form. + // @see Drupal.behaviors.MediaLibraryItemSelection $form['current_selection'] = [ '#type' => 'hidden', '#default_value' => '', @@ -304,7 +305,7 @@ protected function buildCurrentSelectionArea($media_items, array $form, FormStat ], ]; foreach ($media_items as $media_id => $media) { - $selection[$media_id] = $this->buildSelectedItemElement($media); + $selection[$media_id] = $this->buildSelectedItemElement($media, $form, $form_state); } return $selection; } @@ -314,11 +315,15 @@ protected function buildCurrentSelectionArea($media_items, array $form, FormStat * * @param \Drupal\media\MediaInterface $media * The media item. + * @param array $form + * The complete form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current form state. * * @return array * A render array of a selected media item. */ - protected function buildSelectedItemElement($media) { + protected function buildSelectedItemElement(MediaInterface $media, array $form, FormStateInterface $form_state) { return [ '#type' => 'container', '#attributes' => [ @@ -507,8 +512,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) { * The current form state. * * @return array|\Drupal\Core\Ajax\AjaxResponse - * The form array when there are form errors or a AJAX response to select - * the created items in the media library. + * The form array if there are validation errors, or an AJAX response to add + * the created items to the current selection. */ public function updateLibrary(array &$form, FormStateInterface $form_state) { if ($form_state::hasAnyErrors()) { @@ -558,7 +563,7 @@ public function updateWidget(array &$form, FormStateInterface $form_state) { $opener_id = MediaLibraryState::fromRequest($this->getRequest())->getOpenerId(); if ($field_id = MediaLibraryWidget::getOpenerFieldId($opener_id)) { return (new AjaxResponse()) - ->addCommand(new InvokeCommand("[data-media-library-widget-value=\"$field_id\"]", 'val', [implode(',', $this->getAllSelectedMediaIds($form, $form_state))])) + ->addCommand(new InvokeCommand("[data-media-library-widget-value=\"$field_id\"]", 'val', [implode(',', $this->getAllSelectedMediaIds($form_state))])) ->addCommand(new InvokeCommand("[data-media-library-widget-update=\"$field_id\"]", 'trigger', ['mousedown'])) ->addCommand(new CloseDialogCommand()); } @@ -582,15 +587,13 @@ protected function getSourceFieldName(MediaTypeInterface $media_type) { /** * Get all selected media IDs from the form state. * - * @param array $form - * The complete form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current form state. * * @return int[] * An array containing the selected media IDs. */ - protected function getAllSelectedMediaIds(array &$form, FormStateInterface $form_state) { + protected function getAllSelectedMediaIds(FormStateInterface $form_state) { $selected_media_ids = array_filter(explode(',', $form_state->getValue('current_selection'))); $added_media = $form_state->get('media') ?: []; $added_media_ids = array_map(function (MediaInterface $media) { diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php index c47f153652..b25d271297 100644 --- a/core/modules/media_library/src/Form/FileUploadForm.php +++ b/core/modules/media_library/src/Form/FileUploadForm.php @@ -176,7 +176,14 @@ public function validateUploadElement(array $element, FormStateInterface $form_s */ public function processUploadElement(array $element, FormStateInterface $form_state) { $element['upload_button']['#submit'] = ['::uploadButtonSubmit']; - $element['upload_button']['#limit_validation_errors'] = FALSE; + // Limit the validation errors to make sure + // FormValidator::handleErrorsWithLimitedValidation doesn't remove the + // current selection from the form state. + // @see Drupal\Core\Form\FormValidator::handleErrorsWithLimitedValidation() + $element['upload_button']['#limit_validation_errors'] = [ + ['upload'], + ['current_selection'], + ]; $element['upload_button']['#ajax'] = [ 'callback' => '::updateFormCallback', 'wrapper' => 'media-library-wrapper',