diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index d6965560160e94d88ae2f80fec8fc1d33861e65c..93fed859ed5779aa1ea7ea347c2a48b835358472 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -118,7 +118,7 @@ public function form(array $form, FormStateInterface $form_state) { '#attributes' => ['id' => 'source-dependent'], ]; - if ($source) { + if (!$this->entity->isNew()) { $source_description = $this->t('The media source cannot be changed after the media type is created.'); } else { @@ -134,7 +134,7 @@ public function form(array $form, FormStateInterface $form_state) { '#required' => TRUE, // Once the media type is created, its source plugin cannot be changed // anymore. - '#disabled' => !empty($source), + '#disabled' => !$this->entity->isNew(), ]; if ($source) { diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php index 207b542f95f6e000f72a47019ddc743dc12e6da5..42e3511f315b9d1b8a94f43e89663ecd4d932540 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php @@ -11,6 +11,45 @@ */ class MediaTypeCreationTest extends MediaJavascriptTestBase { + /** + * Tests the source field behavior on the add media type form. + */ + public function testSourceChangeOnMediaTypeCreationForm() { + $session = $this->getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + $label = 'Type with Default Field'; + $mediaTypeMachineName = str_replace(' ', '_', strtolower($label)); + + $this->drupalGet('admin/structure/media/add'); + + // Fill in a label to the media type. + $page->fillField('label', $label); + $this->assertNotEmpty( + $assert_session->waitForElementVisible('css', '.machine-name-value') + ); + + // Select the media source used by our media type. + $assert_session->selectExists('Media source')->selectOption('test_different_displays'); + $this->assertNotEmpty( + $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]') + ); + + // Change the media source. + $assert_session->selectExists('Media source')->selectOption('test'); + $this->assertNotEmpty( + $assert_session->waitForElement('css', 'fieldset[data-drupal-selector="edit-source-configuration"] .fieldset-wrapper .placeholder:contains("Text (plain)")') + ); + + $page->pressButton('Save'); + + // Check that source can not be changed anymore. + $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}"); + $assert_session->pageTextContains('The media source cannot be changed after the media type is created'); + $assert_session->fieldDisabled('Media source'); + } + /** * Tests the media type creation form. */