Skip to content
Snippets Groups Projects
Commit 080985dd authored by Guido Schmechel's avatar Guido Schmechel Committed by Benni Mack
Browse files

[BUGFIX] Render hreflang only for translated pages

If a language in the SiteConfig is set to the fallbackType
free and there is no translated page for this language, then
there is now no generated hreflang.

Previously, a link was always generated with default language
slug, which lead to 404 page not found.

Resolves: #94270
Releases: main, 11.5, 10.4
Change-Id: Ifb261cf388fccbaabbf2bcae9ac805b143c9b42f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69919


Tested-by: default avatarcore-ci <typo3@b13.com>
Tested-by: default avatarStefan Bürk <stefan@buerk.tech>
Tested-by: default avatarBenni Mack <benni@typo3.org>
Reviewed-by: default avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: default avatarBenni Mack <benni@typo3.org>
parent b51eeeac
Branches
Tags
No related merge requests found
......@@ -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;
}
......
......@@ -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'}
......@@ -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="',
],
],
];
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment