diff --git a/core/modules/media_library/src/Form/AddFormBase.php b/core/modules/media_library/src/Form/AddFormBase.php index 4428f3bc34..384d17e8f0 100644 --- a/core/modules/media_library/src/Form/AddFormBase.php +++ b/core/modules/media_library/src/Form/AddFormBase.php @@ -149,7 +149,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['description'] = [ '#type' => 'html_tag', '#tag' => 'p', - '#value' => $this->formatPlural(count($added_media), 'The media item has been created. Fill in any required fields and save to make it available in the media library.', 'The media items have been created. Fill in any required fields and save to make them available in the media library.'), + '#value' => $this->formatPlural(count($added_media), 'The media item has been created. Fill in any required fields and save to add it to the media library.', 'The media items have been created. Fill in any required fields and save to add them to the media library.'), '#attributes' => [ 'class' => [ 'media-library-add-form-description', @@ -570,6 +570,9 @@ public function updateWidget(array &$form, FormStateInterface $form_state) { // Pass the selection to the field widget based on the current widget ID. $opener_id = MediaLibraryState::fromRequest($this->getRequest())->getOpenerId(); if ($field_id = MediaLibraryWidget::getOpenerFieldId($opener_id)) { + // The added media items get an ID when they are saved in ::submitForm(). + // For that reason the added media items are keyed by delta in the form + // state and we have to do an array map to get each media ID. $current_media_ids = array_map(function (MediaInterface $media) { return $media->id(); }, $this->getCurrentMediaItems($form_state)); @@ -602,7 +605,7 @@ protected function getSourceFieldName(MediaTypeInterface $media_type) { * The current form state. * * @return \Drupal\media\MediaInterface[] - * An array containing the pre-selected media items. + * An array containing the pre-selected media items keyed by ID. */ protected function getPreSelectedMediaItems(FormStateInterface $form_state) { $media_ids = array_filter(explode(',', $form_state->getValue('current_selection'))); @@ -619,7 +622,9 @@ protected function getPreSelectedMediaItems(FormStateInterface $form_state) { * The current form state. * * @return \Drupal\media\MediaInterface[] - * An array containing all pre-selected and added media items. + * An array containing all pre-selected and added media items. The + * pre-selected media items are keyed by ID and the added items are keyed by + * delta. */ protected function getCurrentMediaItems(FormStateInterface $form_state) { $pre_selected_media = $this->getPreSelectedMediaItems($form_state);