diff --git a/core/modules/media/src/Controller/MediaFilterController.php b/core/modules/media/src/Controller/MediaFilterController.php index 59c263e1bc..f5e36d704c 100644 --- a/core/modules/media/src/Controller/MediaFilterController.php +++ b/core/modules/media/src/Controller/MediaFilterController.php @@ -5,6 +5,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\ContentEntityStorageInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Render\RendererInterface; use Drupal\filter\FilterFormatInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -36,6 +37,13 @@ class MediaFilterController implements ContainerInjectionInterface { */ protected $mediaStorage; + /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + /** * Constructs an MediaFilterController instance. * @@ -43,10 +51,13 @@ class MediaFilterController implements ContainerInjectionInterface { * The renderer service. * @param \Drupal\Core\Entity\ContentEntityStorageInterface * The media storage. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct(RendererInterface $renderer, ContentEntityStorageInterface $media_storage) { + public function __construct(RendererInterface $renderer, ContentEntityStorageInterface $media_storage, EntityRepositoryInterface $entity_repository) { $this->renderer = $renderer; $this->mediaStorage = $media_storage; + $this->entityRepository = $entity_repository; } /** @@ -55,7 +66,8 @@ public function __construct(RendererInterface $renderer, ContentEntityStorageInt public static function create(ContainerInterface $container) { return new static( $container->get('renderer'), - $container->get('entity_type.manager')->getStorage('media') + $container->get('entity_type.manager')->getStorage('media'), + $container->get('entity.repository') ); } @@ -96,10 +108,13 @@ public function preview(Request $request, FilterFormatInterface $filter_format) // Use the bubbled cache tags to determine which media was rendered (if any) // so we can embed the label in the response, for use in an ARIA label. $headers = []; - $media_cache_tags = array_filter($build['#cache']['tags'], function ($t) { return strpos($t, 'media:') === 0;}); + $media_cache_tags = array_filter($build['#cache']['tags'], function ($t) { + return strpos($t, 'media:') === 0; + }); if (!empty($media_cache_tags)) { $embedded_media_id = substr(reset($media_cache_tags), 6); - $headers['Drupal-Media-Label'] = $this->mediaStorage->load($embedded_media_id)->label(); + $media = $this->mediaStorage->load($embedded_media_id); + $headers['Drupal-Media-Label'] = $this->entityRepository->getTranslationFromContext($media)->label(); } // Note that we intentionally do not use: diff --git a/core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php index 5bd2990e40..85dc82f9df 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php @@ -275,6 +275,8 @@ public function testEditableCaption() { $this->assignNameToCkeditorIframe(); $this->getSession()->switchToIFrame('ckeditor'); $this->assertNotEmpty($assert_session->waitForButton('Edit media')); + // Test `aria-label` attribute appears on the widget wrapper. + $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Screaming hairy armadillo"]'); $assert_session->elementContains('css', 'figcaption', ''); $assert_session->elementAttributeContains('css', 'figcaption', 'data-placeholder', 'Enter caption here'); // Test if you leave the caption blank, but change another attribute, @@ -464,6 +466,8 @@ public function testDialogAccess() { $this->assignNameToCkeditorIframe(); $this->getSession()->switchToIFrame('ckeditor'); $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media', 2000)); + // Test `aria-label` attribute appears on the widget wrapper. + $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Screaming hairy armadillo"]'); $page->pressButton('Edit media'); $this->waitForMetadataDialog(); $assert_session->fieldNotExists('attributes[alt]'); @@ -624,6 +628,8 @@ public function testAlt() { // Assert that the img within the media embed within the CKEditor contains // the overridden alt text set in the dialog. $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media img[alt*="' . $who_is_zartan . '"]')); + // Test `aria-label` attribute appears on the widget wrapper. + $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Screaming hairy armadillo"]'); // Test that the downcast drupal-media element now has the alt attribute // entered in the dialog. @@ -758,6 +764,8 @@ public function testTranslationAlt() { // Test that the default alt attribute displays without an override. $this->assertNotEmpty($assert_session->waitForElementVisible('xpath', '//img[contains(@alt, "texte alternatif par défaut")]')); + // Test `aria-label` attribute appears on the widget wrapper. + $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Tatou poilu hurlant"]'); $page->pressButton('Edit media'); $this->waitForMetadataDialog(); // Assert that the placeholder is set to the value of the media field's @@ -1137,6 +1145,7 @@ public function testViewMode() { $this->assignNameToCkeditorIframe(); $this->getSession()->switchToIFrame('ckeditor'); $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media')); + $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Screaming hairy armadillo"]'); $page->pressButton('Edit media'); $this->waitForMetadataDialog(); $assert_session->optionExists('attributes[data-view-mode]', 'view_mode_1');