diff --git a/core/modules/media/media.module b/core/modules/media/media.module index ecf2d6628df56620b2ae6f7424247ec90b7c06af..116c6f50a6a311654c661a9637ff8bb4f0ae1b20 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -16,7 +16,6 @@ use Drupal\Core\Template\Attribute; use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; -use Drupal\media\Plugin\media\Source\Image; use Drupal\media\Plugin\media\Source\OEmbedInterface; use Drupal\views\ViewExecutable; @@ -517,36 +516,6 @@ function media_field_widget_form_alter(&$element, FormStateInterface $form_state } } -/** - * Implements hook_form_FORM_ID_alter(). - */ -function media_form_media_type_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) { - $form['actions']['submit']['#submit'][] = '_media_type_add_form_submit'; -} - -/** - * Submit callback for media_type_add_form. - */ -function _media_type_add_form_submit(array &$form, FormStateInterface $form_state) { - $type = $form_state->getFormObject()->getEntity(); - - if (!is_a($type->getSource(), Image::class, TRUE)) { - return; - } - - // Load the default entity view display. - $display = \Drupal::service('entity_display.repository') - ->getViewDisplay('media', $type->id()); - - // Remove all default components. - foreach (array_keys($display->getComponents()) as $name) { - $display->removeComponent($name); - } - $type->getSource()->prepareViewDisplay($type, $display); - - $display->save(); -} - /** * Implements hook_views_query_substitutions(). */ diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php index c01b9946ea0f8e54330acd3ba50ef7fd2d2cb8ce..5da82a64439f727127751f1a2c5908e9eaf3072b 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -339,7 +339,9 @@ public function getSourceFieldValue(MediaInterface $media) { * {@inheritdoc} */ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { - $display->setComponent($this->getSourceFieldDefinition($type)->getName()); + $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ + 'label' => 'visually_hidden', + ]); } /** diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index 603af18f167874137a1a8127b26ce6226b06c17d..289fc8aad080fc1414faa9dd5bb00e3b44a51fd0 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -363,6 +363,11 @@ public function save(array $form, FormStateInterface $form_state) { } if ($source_field->isDisplayConfigurable('view')) { $display = $this->entityDisplayRepository->getViewDisplay('media', $media_type->id()); + + // Remove all default components. + foreach (array_keys($display->getComponents()) as $name) { + $display->removeComponent($name); + } $source->prepareViewDisplay($media_type, $display); $display->save(); } diff --git a/core/modules/media/src/Plugin/media/Source/AudioFile.php b/core/modules/media/src/Plugin/media/Source/AudioFile.php index 22291434e971f7d91877472e495bff216549ddc2..1e215930851d2da5f3dc262d0e5fdd04195ecba5 100644 --- a/core/modules/media/src/Plugin/media/Source/AudioFile.php +++ b/core/modules/media/src/Plugin/media/Source/AudioFile.php @@ -33,6 +33,7 @@ public function createSourceField(MediaTypeInterface $type) { public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ 'type' => 'file_audio', + 'label' => 'visually_hidden', ]); } diff --git a/core/modules/media/src/Plugin/media/Source/Image.php b/core/modules/media/src/Plugin/media/Source/Image.php index a6e6cac25e6773b6b17270ccb65914cac6e96025..be37177f2dc61a2c4037649c951487c4d228a077 100644 --- a/core/modules/media/src/Plugin/media/Source/Image.php +++ b/core/modules/media/src/Plugin/media/Source/Image.php @@ -174,7 +174,6 @@ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayIn // set an image style. $field_name = $this->getSourceFieldDefinition($type)->getName(); $component = $display->getComponent($field_name); - $component['label'] = 'visually_hidden'; $component['settings']['image_link'] = ''; $component['settings']['image_style'] = ''; if ($this->entityTypeManager->getStorage('image_style')->load('large')) { diff --git a/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php index a3d93ff354417613128115bd9542edabd661fc62..9fbea0b16c6ed116817d6ebf0ee9eb184f8888ab 100644 --- a/core/modules/media/src/Plugin/media/Source/OEmbed.php +++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php @@ -442,6 +442,7 @@ public function getSourceFieldConstraints() { public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ 'type' => 'oembed', + 'label' => 'visually_hidden', ]); } diff --git a/core/modules/media/src/Plugin/media/Source/VideoFile.php b/core/modules/media/src/Plugin/media/Source/VideoFile.php index 214d36c41a3d68a85b337e4f7b812ecf6b384039..ebcf2f474a38353c76094e6e8e3292c24264bc72 100644 --- a/core/modules/media/src/Plugin/media/Source/VideoFile.php +++ b/core/modules/media/src/Plugin/media/Source/VideoFile.php @@ -33,6 +33,7 @@ public function createSourceField(MediaTypeInterface $type) { public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ 'type' => 'file_video', + 'label' => 'visually_hidden', ]); } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php index fd55af8406714050f7789f5150d9ad4832460fcf..e77c5203cd9c464e99e990366179b836a73aca62 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php @@ -62,8 +62,10 @@ public function testMediaFileSource() { // Get the media entity view URL from the creation message. $this->drupalGet($this->assertLinkToCreatedMedia()); - // Make sure the thumbnail is displayed. - $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png'); + // Make sure a link to the file is displayed. + $assert_session->linkExists($test_filename); + // The thumbnail should not be displayed. + $assert_session->elementNotExists('css', '.image-style-thumbnail'); // Make sure checkbox changes the visibility of log message field. $this->drupalGet("media/1/edit"); diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php index 42817744a826246a0a3a2f683f8468d0ed3d3da2..91f47ca74dc4ab0b668c264390219b3a723026a7 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php @@ -141,8 +141,10 @@ public function testMediaOEmbedVideoSource() { $this->assertSame('480', $session->evaluateScript("$inner_frame.getAttribute('width')")); $this->assertLessThanOrEqual(240, $session->evaluateScript("$inner_frame.clientWidth")); - // Make sure the thumbnail is displayed from uploaded image. - $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', '/oembed_thumbnails/' . basename($thumbnail)); + // The oEmbed content iFrame should be visible. + $assert_session->elementExists('css', 'iframe.media-oembed-content'); + // The thumbnail should not be displayed. + $assert_session->elementNotExists('css', '.image-style-thumbnail'); // Load the media and check that all fields are properly populated. $media = Media::load(1); diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php index 53ea9bf690cb08bcf380938a0d1a9c81191d21fe..8b28129c4f02920e72dd51fac43760a1eba6a8b1 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php @@ -113,11 +113,14 @@ protected function hideMediaTypeFieldWidget($field_name, $media_type_id) { * The media source ID. * @param array $provided_fields * (optional) An array of field machine names this type provides. + * @param string $source_label_visibility + * (optional) The visibility that the source field label is expected to + * have. Defaults to 'visually_hidden'. * * @return \Drupal\media\MediaTypeInterface * The created media type. */ - public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = []) { + public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = [], $source_label_visibility = 'visually_hidden') { $session = $this->getSession(); $page = $session->getPage(); $assert_session = $this->assertSession(); @@ -146,12 +149,27 @@ public function doTestCreateMediaType($media_type_id, $source_id, array $provide $this->drupalGet('admin/structure/media'); $assert_session->pageTextContains($media_type_id); + $media_type = MediaType::load($media_type_id); + + // Assert that the default display of the media type only shows the source + // field. + $this->drupalGet("/admin/structure/media/manage/$media_type_id/display"); + // There should be only one field with editable settings, and it should be + // the source field. + $assert_session->elementsCount('css', 'input[name$="_settings_edit"]', 1); + $source_field_name = $media_type->getSource() + ->getSourceFieldDefinition($media_type) + ->getName(); + $assert_session->buttonExists("{$source_field_name}_settings_edit"); + // Ensure the source field label is configured as expected. + $assert_session->fieldValueEquals("fields[$source_field_name][label]", $source_label_visibility); + // Bundle definitions are statically cached in the context of the test, we // need to make sure we have updated information before proceeding with the // actions on the UI. \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); - return MediaType::load($media_type_id); + return $media_type; } }