diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index 170a34fa5c2e71fd709e4c6010199d6efad5a0ae..7766af2e567d63be3dd65acd453ecaf0cd21d6ba 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -131,6 +131,13 @@ public function form(array $form, FormStateInterface $form_state) { '#options' => $options, '#description' => $source_description, '#ajax' => ['callback' => '::ajaxHandlerData'], + // Rebuilding the form as part of the AJAX request is a workaround to + // enforce machine_name validation. + // @todo This was added as part of #2932226 and it should be removed once + // https://www.drupal.org/project/drupal/issues/2557299 solves it in a + // more generic way. + '#executes_submit_callback' => TRUE, + '#submit' => [[static::class, 'rebuildSubmit']], '#required' => TRUE, // Once the media type is created, its source plugin cannot be changed // anymore. @@ -233,6 +240,18 @@ public function form(array $form, FormStateInterface $form_state) { return $form; } + /** + * Form submission handler to rebuild the form on select submit. + * + * @param array $form + * Full form array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Current form state. + */ + public static function rebuildSubmit(array &$form, FormStateInterface $form_state) { + $form_state->setRebuild(); + } + /** * Prepares workflow options to be used in the 'checkboxes' form element. * @@ -273,7 +292,7 @@ protected function getSourceSubFormState(array $form, FormStateInterface $form_s public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); - if ($form['source_dependent']['source_configuration']) { + if (isset($form['source_dependent']['source_configuration'])) { // Let the selected plugin validate its settings. $this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state)); } @@ -296,7 +315,7 @@ function ($item) { ->setStatus((bool) $form_state->getValue(['options', 'status'])) ->setNewRevision((bool) $form_state->getValue(['options', 'new_revision'])); - if ($form['source_dependent']['source_configuration']) { + if (isset($form['source_dependent']['source_configuration'])) { // Let the selected plugin save its settings. $this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state)); } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php index 652aca8c6be17c732feccf4c509751e5429bb138..4f491ccb4a07aabb61cb1bc0710a7177a9cd57f3 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php @@ -53,6 +53,15 @@ public function testMediaTypeCreationFormWithDefaultField() { // Check that the plugin cannot be changed after it is set on type creation. $assert_session->fieldDisabled('Media source'); $assert_session->pageTextContains('The media source cannot be changed after the media type is created.'); + + // Check that a new type with the same machine name cannot be created. + $this->drupalGet('admin/structure/media/add'); + $page->fillField('label', $label); + $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'"); + $page->selectFieldOption('Media source', 'test'); + $assert_session->assertWaitOnAjaxRequest(); + $page->pressButton('Save'); + $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.'); } /**