diff --git a/core/modules/media/media.module b/core/modules/media/media.module index ecf2d6628df..116c6f50a6a 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -16,7 +16,6 @@ use Drupal\Core\Session\AccountInterface; 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 c01b9946ea0..5da82a64439 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -339,7 +339,9 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac * {@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 603af18f167..289fc8aad08 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -363,6 +363,11 @@ class MediaTypeForm extends EntityForm { } 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 22291434e97..1e215930851 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 @@ class AudioFile extends File { 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 a6e6cac25e6..be37177f2dc 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 @@ class Image extends File { // 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 a3d93ff3544..9fbea0b16c6 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 @@ class OEmbed extends MediaSourceBase implements OEmbedInterface { 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 214d36c41a3..ebcf2f474a3 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 @@ class VideoFile extends File { 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 fd55af84067..e77c5203cd9 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php @@ -62,8 +62,10 @@ class MediaSourceFileTest extends MediaSourceTestBase { // 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 42817744a82..91f47ca74dc 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php @@ -141,8 +141,10 @@ class MediaSourceOEmbedVideoTest extends MediaSourceTestBase { $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 53ea9bf690c..8b28129c4f0 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php @@ -113,11 +113,14 @@ abstract class MediaSourceTestBase extends MediaJavascriptTestBase { * 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 @@ abstract class MediaSourceTestBase extends MediaJavascriptTestBase { $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; } }