diff --git a/core/modules/media_library/src/Form/AddFormBase.php b/core/modules/media_library/src/Form/AddFormBase.php index 4e175a9a39..5fa5038659 100644 --- a/core/modules/media_library/src/Form/AddFormBase.php +++ b/core/modules/media_library/src/Form/AddFormBase.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; use Drupal\media\MediaInterface; use Drupal\media\MediaTypeInterface; use Drupal\media_library\Ajax\UpdateSelectionCommand; @@ -125,6 +126,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['#suffix'] = ''; $form['#attached']['library'][] = 'media_library/style'; + // Since the media library can be loaded via AJAX, we force the action URL + // of the add form to the media library controller so we now for sure the + // form is actually on the page. + $form['#action'] = Url::fromRoute('media_library.ui', [], [ + 'query' => $form_state->get('media_library_state')->all(), + ])->toString(); + // The form is posted via AJAX. When there are messages set during the // validation or submission of the form, the messages need to be shown to // the user. @@ -585,7 +593,7 @@ 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(); + $opener_id = $form_state->get('media_library_state')->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 diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php index 2a306b9ebe..f28376e237 100644 --- a/core/modules/media_library/src/Form/FileUploadForm.php +++ b/core/modules/media_library/src/Form/FileUploadForm.php @@ -8,9 +8,11 @@ use Drupal\Core\Field\TypedData\FieldItemDataDefinition; use Drupal\Core\File\Exception\FileWriteException; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Url; use Drupal\file\FileInterface; use Drupal\file\Plugin\Field\FieldType\FileFieldItemList; use Drupal\file\Plugin\Field\FieldType\FileItem; @@ -200,6 +202,16 @@ public function processUploadElement(array $element, FormStateInterface $form_st $element['upload_button']['#ajax'] = [ 'callback' => '::updateFormCallback', 'wrapper' => 'media-library-wrapper', + // Add a fixed URL to post the form since AJAX forms are automatically + // posted to instead of $form['#action']. + // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115 + // is fixed. + 'url' => Url::fromRoute('media_library.ui'), + 'options' => [ + 'query' => $form_state->get('media_library_state')->all() + [ + FormBuilderInterface::AJAX_FORM_REQUEST => TRUE, + ], + ], ]; return $element; } diff --git a/core/modules/media_library/src/Form/OEmbedForm.php b/core/modules/media_library/src/Form/OEmbedForm.php index 0361c1f0e9..ffdc6950c9 100644 --- a/core/modules/media_library/src/Form/OEmbedForm.php +++ b/core/modules/media_library/src/Form/OEmbedForm.php @@ -3,7 +3,9 @@ namespace Drupal\media_library\Form; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; use Drupal\media\OEmbed\ResourceException; use Drupal\media\OEmbed\ResourceFetcherInterface; use Drupal\media\OEmbed\UrlResolverInterface; @@ -121,6 +123,16 @@ protected function buildInputElement(array $form, FormStateInterface $form_state '#ajax' => [ 'callback' => '::updateFormCallback', 'wrapper' => 'media-library-wrapper', + // Add a fixed URL to post the form since AJAX forms are automatically + // posted to instead of $form['#action']. + // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115 + // is fixed. + 'url' => Url::fromRoute('media_library.ui'), + 'options' => [ + 'query' => $form_state->get('media_library_state')->all() + [ + FormBuilderInterface::AJAX_FORM_REQUEST => TRUE, + ], + ], ], '#attributes' => [ 'class' => ['media-library-add-form-oembed-submit'], diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php index e2292e3b73..043c5daf0c 100644 --- a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php +++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php @@ -723,7 +723,7 @@ public function testWidgetUpload() { $assert_session->pageTextNotContains($png_image->filename); // Assert we can also directly insert uploaded files in the widget. - $assert_session->elementExists('css', '.media-library-open-button[href*="field_twin_media"]')->click(); + $assert_session->elementExists('css', '.media-library-open-button[name^="field_twin_media"]')->click(); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains('Add or select media'); $page->clickLink('Type Three'); @@ -839,7 +839,7 @@ public function testWidgetUpload() { // Assert we can also remove selected items from the selection area in the // upload form. - $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]')->click(); + $assert_session->elementExists('css', '.media-library-open-button[name^="field_unlimited_media"]')->click(); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains('Add or select media'); $page->clickLink('Type Three'); @@ -1007,7 +1007,7 @@ public function testWidgetOEmbed() { $assert_session->pageTextContains('Custom video title'); // Assert we can directly insert added oEmbed media in the widget. - $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]')->click(); + $assert_session->elementExists('css', '.media-library-open-button[name^="field_unlimited_media"]')->click(); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains('Add or select media'); $page->clickLink('Type Five'); @@ -1022,7 +1022,7 @@ public function testWidgetOEmbed() { // Assert we can remove selected items from the selection area in the oEmbed // form. - $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]')->click(); + $assert_session->elementExists('css', '.media-library-open-button[name^="field_unlimited_media"]')->click(); $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains('Add or select media'); $page->clickLink('Type Five');