.../modules/media/src/Plugin/Filter/MediaEmbed.php | 1 + .../src/Kernel/MediaEmbedFilterTranslationTest.php | 90 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/core/modules/media/src/Plugin/Filter/MediaEmbed.php b/core/modules/media/src/Plugin/Filter/MediaEmbed.php index ee2f9eb8c7..db8cff0579 100644 --- a/core/modules/media/src/Plugin/Filter/MediaEmbed.php +++ b/core/modules/media/src/Plugin/Filter/MediaEmbed.php @@ -172,6 +172,7 @@ public function process($text, $langcode) { \Drupal::logger('media')->error('The media item with UUID "@uuid" does not exist.', ['@uuid' => $uuid]); } else { + $media = $this->entityRepository->getTranslationFromContext($media, $langcode); $this->applyPerEmbedMediaOverrides($node, $media); } diff --git a/core/modules/media/tests/src/Kernel/MediaEmbedFilterTranslationTest.php b/core/modules/media/tests/src/Kernel/MediaEmbedFilterTranslationTest.php new file mode 100644 index 0000000000..e121b782ee --- /dev/null +++ b/core/modules/media/tests/src/Kernel/MediaEmbedFilterTranslationTest.php @@ -0,0 +1,90 @@ +save(); + // Reload the entity to ensure it is aware of the newly created language. + $this->embeddedEntity = $this->container->get('entity.manager') + ->getStorage('media') + ->load($this->embeddedEntity->id()); + + $this->embeddedEntity->addTranslation('pt-br') + ->set('field_media_image', [ + 'target_id' => $this->image->id(), + 'alt' => 'pt-br alt', + 'title' => 'pt-br title', + ])->save(); + } + + /** + * Tests that the expected embedded media entity translation is selected. + * + * @dataProvider providerTranslationSituations + */ + public function testTranslationSelection($text_langcode, $expected_title_langcode) { + $text = $this->createEmbedCode([ + 'data-entity-type' => 'media', + 'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID, + 'data-view-mode' => 'full', + ]); + + $result = $this->processText($text, $text_langcode, ['media_embed']); + $this->setRawContent($result->getProcessedText()); + + $this->assertSame( + $this->embeddedEntity->getTranslation($expected_title_langcode)->field_media_image->alt, + (string) $this->cssSelect('img')[0]->attributes()['alt'] + ); + // Verify that the filtered text does not vary by translation-related cache + // contexts: a particular translation of the embedded entity is selected + // based on either the host entity's language, which should require a cache + // context to be associated. (The host entity's language may itself be + // selected based on some request context, but that is of no concern to + // this filter.) + $this->assertSame($result->getCacheContexts(), ['timezone', 'user.permissions']); + } + + /** + * Data provider for testTranslationSelection(). + */ + public function providerTranslationSituations() { + $embedded_entity_translation_languages = ['en', 'pt-br']; + + foreach (['en', 'pt-br', 'nl'] as $text_langcode) { + // The text language (which is set to the host entity's language) must be + // respected in selecting a translation. If that translation does not + // exist, it falls back to the default translation of the embedded entity. + $match_or_fallback_langcode = in_array($text_langcode, $embedded_entity_translation_languages) + ? $text_langcode + : 'en'; + yield "text_langcode=$text_langcode ⇒ $match_or_fallback_langcode" => [ + $text_langcode, + $match_or_fallback_langcode, + ]; + } + } + +}