diff -u b/core/modules/media/js/commands.es6.js b/core/modules/media/js/commands.es6.js --- b/core/modules/media/js/commands.es6.js +++ b/core/modules/media/js/commands.es6.js @@ -24,7 +24,14 @@ progress: { type: 'throbber' } - }).execute(); + }).execute().done((data, textStatus, jqXHR) => { + // Warn the user when closing the dialog. + $('#drupal-modal').on('dialogbeforeclose', (event, ui) => { + if (event.originalEvent) { + return confirm(Drupal.t("Closing this modal will cancel the upload process.\n\nAre you sure you want to close?")); + } + }); + }); }; /** diff -u b/core/modules/media/js/commands.js b/core/modules/media/js/commands.js --- b/core/modules/media/js/commands.js +++ b/core/modules/media/js/commands.js @@ -14,7 +14,13 @@ progress: { type: 'throbber' } - }).execute(); + }).execute().done(function (data, textStatus, jqXHR) { + $('#drupal-modal').on('dialogbeforeclose', function (event, ui) { + if (event.originalEvent) { + return confirm(Drupal.t("Closing this modal will cancel the upload process.\n\nAre you sure you want to close?")); + } + }); + }); }; Drupal.AjaxCommands.prototype.addMediaToWidget = function (ajax, response, status) { diff -u b/core/modules/media/src/Form/MediaBulkUploadForm.php b/core/modules/media/src/Form/MediaBulkUploadForm.php --- b/core/modules/media/src/Form/MediaBulkUploadForm.php +++ b/core/modules/media/src/Form/MediaBulkUploadForm.php @@ -104,6 +104,15 @@ return $form; } + // If only one type accepts the current file, skip the type selection. + if ($form_state->get('step') === 'select_media') { + $file = $form_state->get('current_file'); + $valid_types = $this->filterTypesThatAcceptFile($file, $types); + if (count($valid_types) === 1) { + self::mediaTypeSelectSubmit($form, $form_state, reset($valid_types)); + } + } + $form['progress'] = $this->getFormProgress($form_state); // Determine what step of the form we're on. switch ($form_state->get('step')) { @@ -261,10 +270,16 @@ protected function buildMediaForm(array $form, FormStateInterface $form_state) { /** @var \Drupal\Core\Entity\EntityFormInterface $media_form */ $media_form = $form_state->get('media_form'); + if (!empty($form_state->get('files'))) { + $save_value = $this->t('Save and continue'); + } + else { + $save_value = $this->t('Save and finish'); + } $form['subform'] = $media_form->buildForm([], $form_state); $form['subform']['actions']['submit'] = [ '#type' => 'submit', - '#value' => $this->t('Save and continue'), + '#value' => $save_value, '#submit' => [[static::class, 'mediaFormSubmit']], '#ajax' => [ 'callback' => [static::class, 'mediaFormSubmitCallback'], @@ -334,16 +349,27 @@ $form_object = $form_state->getFormObject(); $media_form = $form_object->createMediaForm($file, $type); $storage = $form_state->getStorage(); - // Remove storage values set by the previous media form - this prevents - // previously submitted values from appearing in new forms. - $storage = array_diff_key($storage, array_flip([ - 'entity_default_langcode', - 'langcode', - 'form_display', - 'entity_form_initialized', - 'field_storage', + // Remove all unknown values from form state, to ensure a clean media form. + $storage = array_intersect_key($storage, array_flip([ + 'total_files', + 'current_file', + 'files', + 'step', + 'media_type', + 'media_items', ])); $form_state->setStorage($storage); + $input = $form_state->getUserInput(); + $input = array_intersect_key($input, array_flip([ + 'form_build_id', + 'form_token', + 'form_id', + 'ajax_page_state', + '_drupal_ajax', + '_triggering_element_name', + '_triggering_element_value', + ])); + $form_state->setUserInput($input); $form_state->set('media_form', $media_form); $form_state->setRebuild(); @@ -363,6 +389,7 @@ public function createMediaForm(FileInterface $file, MediaTypeInterface $type) { // Move the temporary file to the correct destination. $location = $this->getUploadLocationForType($type); + file_prepare_directory($location, FILE_CREATE_DIRECTORY); file_move($file, $location); $media_form = $this->entityTypeManager->getFormObject('media', 'add'); /** @var \Drupal\media\MediaInterface $media */