diff --git a/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php b/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php index 741129faa95fe89e9b99912ab96b748953a7fefb..a77c254d4bcf273c8ba14c4f1266349946df8fcf 100644 --- a/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php +++ b/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php @@ -70,8 +70,12 @@ class HrefLangGenerator foreach ($languages['languagemenu'] as $language) { if (!empty($language['link']) && $language['hreflang']) { $page = $this->getTranslatedPageRecord($pageId, $language['languageId'], $site); + // do not set hreflang if a page is not translated explicitly + if (empty($page)) { + continue; + } + // do not set hreflang when canonical is set explicitly if (!empty($page['canonical_link'])) { - // do not set hreflang when canonical is set continue; } diff --git a/typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml b/typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml index 881af1154fa51c9676d88eadacd5b040214f978d..e987d52e2fb1bc259fc49aaba938d619650eb723 100644 --- a/typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml +++ b/typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml @@ -32,6 +32,8 @@ entitySettings: entities: page: - self: {id: *idAcmeRootPage, title: 'EN: Root', root: true, slug: '/'} + languageVariants: + - self: {id: 1001, title: 'DE-CH: Root', language: 2, slug: '/'} children: - self: {id: 1100, title: 'EN: Welcome', slug: '/hello'} languageVariants: @@ -53,3 +55,6 @@ entities: languageVariants: - self: {id: 1501, title: 'DE: Kein hreflang', language: 1, slug: '/kein-hreflang'} - self: {id: 1502, title: 'DE-CH: Kein hreflang', language: 2, slug: '/kein-hreflang'} + - self: {id: 1600, title: 'EN: No translation', slug: '/no-translation'} + languageVariants: + - self: {id: 1601, title: 'DK: Hidden page', language: 5, hidden: 1, slug: '/no-translation'} diff --git a/typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php b/typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php index 3aa07eaab6571ff24a4465e62710f2347b1893fe..8b939b3c582c79a20b9e5fe469bd6510c1601ea9 100644 --- a/typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php +++ b/typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php @@ -42,6 +42,7 @@ class HrefLangGeneratorTest extends FunctionalTestCase 'DE-CH' => ['id' => 2, 'title' => 'Swiss German', 'locale' => 'de_CH.UTF8', 'iso' => 'de', 'hrefLang' => 'de-CH', 'direction' => ''], 'NL' => ['id' => 3, 'title' => 'Dutch', 'locale' => 'nl_NL.UTF8', 'iso' => 'nl', 'hrefLang' => '', 'direction' => ''], 'FR' => ['id' => 4, 'title' => 'French', 'locale' => 'fr_FR.UTF8', 'iso' => 'fr', 'hrefLang' => 'fr-FR', 'direction' => ''], + 'DK' => ['id' => 5, 'title' => 'Danish', 'locale' => 'da_DK.UTF8', 'iso' => 'da', 'hrefLang' => 'da-DK', 'direction' => ''], ]; protected function setUp(): void @@ -57,6 +58,7 @@ class HrefLangGeneratorTest extends FunctionalTestCase $this->buildLanguageConfiguration('DE-CH', '/de-ch', ['DE'], 'fallback'), $this->buildLanguageConfiguration('NL', '/nl'), $this->buildLanguageConfiguration('FR', '/fr'), + $this->buildLanguageConfiguration('DK', '/dk', ['EN'], 'free'), ] ); @@ -64,13 +66,10 @@ class HrefLangGeneratorTest extends FunctionalTestCase } /** - * @param string $url - * @param array $expected - * * @test * @dataProvider checkHrefLangOutputDataProvider */ - public function checkHrefLangOutput($url, $expectedTags, $notExpectedTags): void + public function checkHrefLangOutput(string $url, array $expectedTags, array $notExpectedTags): void { $this->setUpFrontendRootPage( 1000, @@ -206,6 +205,13 @@ class HrefLangGeneratorTest extends FunctionalTestCase '<link rel="alternate" hreflang="', ], ], + 'Languages with fallback type free should not have hreflang when page record is not translated' => [ + 'https://acme.com/no-translation', + [], + [ + '<link rel="alternate" hreflang="', + ], + ], ]; }