From 17b0786b28c18989bf2d1e234cfd2da0658ac810 Mon Sep 17 00:00:00 2001
From: Markus Klein <markus.klein@typo3.org>
Date: Fri, 27 Mar 2020 12:13:00 +0100
Subject: [PATCH] [BUGFIX] Do not generate links to unreachable pages
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix the condition whether a page is linkable in the requested
language or not.

Resolves: #90850
Releases: master, 9.5, 8.7
Change-Id: I720c09cc1f938b5000980bcba20786a5cf41f98d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63946
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Christian Eßl <indy.essl@gmail.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Josef Glatz <josefglatz@gmail.com>
Reviewed-by: Guido Schmechel <guido.schmechel@brandung.de>
Reviewed-by: Christian Eßl <indy.essl@gmail.com>
---
 .../frontend/Classes/Typolink/PageLinkBuilder.php      | 10 ++++++----
 .../LocalizedPageRendering/ScenarioDTest.php           |  3 +--
 .../LocalizedPageRendering/ScenarioETest.php           |  6 ++----
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
index 8afb4ca43cf1..864bf12f4f38 100644
--- a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
+++ b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
@@ -196,12 +196,14 @@ class PageLinkBuilder extends AbstractTypolinkBuilder
             }
             // Check if the target page can be access depending on l18n_cfg
             if (!$tsfe->sys_page->isPageSuitableForLanguage($page, $languageAspect)) {
-                $languageField = $GLOBALS['TCA']['pages']['ctrl']['languageField'] ?? null;
-                $languageOfPageRecord = (int)($page[$languageField] ?? 0);
-                if ($languageOfPageRecord === 0 && GeneralUtility::hideIfDefaultLanguage($page['l18n_cfg'])) {
+                if ($siteLanguageOfTargetPage->getLanguageId() === 0 && GeneralUtility::hideIfDefaultLanguage($page['l18n_cfg'])) {
                     throw new UnableToLinkException('Default language of page  "' . $linkDetails['typoLinkParameter'] . '" is hidden, so "' . $linkText . '" was not linked.', 1551621985, null, $linkText);
                 }
-                if ($languageOfPageRecord > 0 && !isset($page['_PAGES_OVERLAY']) && GeneralUtility::hideIfNotTranslated($page['l18n_cfg'])) {
+                // If the requested language is not the default language and the page has no overlay for this language
+                // generating a link would cause a 404 error when using this like if one of those conditions apply:
+                //  - The page is set to be hidden if it is not translated (evaluated in TSFE)
+                //  - The site configuration has a "strict" fallback set (evaluated in the Router - very early)
+                if ($siteLanguageOfTargetPage->getLanguageId() > 0 && !isset($page['_PAGES_OVERLAY']) && (GeneralUtility::hideIfNotTranslated($page['l18n_cfg']) || $siteLanguageOfTargetPage->getFallbackType() === 'strict')) {
                     throw new UnableToLinkException('Fallback to default language of page "' . $linkDetails['typoLinkParameter'] . '" is disabled, so "' . $linkText . '" was not linked.', 1551621996, null, $linkText);
                 }
             }
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
index 684cb8ac26b6..d7439c8e6e9a 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
@@ -167,8 +167,7 @@ class ScenarioDTest extends AbstractLocalizedPagesTestCase
                 'menu' => [
                     ['title' => 'DE-CH: Ãœber uns', 'link' => '/de-ch/ueber-uns'],
                     ['title' => 'DE-CH: Produkte', 'link' => '/de-ch/produkte'],
-                    // FIXME: Page "EN: Shortcut to welcome" must to be rendered in menu, needs a refactored menu generation
-                    ['title' => 'EN: Shortcut to welcome', 'link' => '/de-ch/hello'],
+                    ['title' => 'EN: Shortcut to welcome', 'link' => ''],
                 ],
             ],
         ];
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioETest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioETest.php
index 601b0af3b160..1fce95ca7808 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioETest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioETest.php
@@ -152,8 +152,7 @@ class ScenarioETest extends AbstractLocalizedPagesTestCase
                 'menu' => [
                     ['title' => 'DE: Ãœber uns', 'link' => '/de-ch/ueber-uns'],
                     ['title' => 'DE: Produkte', 'link' => '/de-ch/produkte'],
-                    // FIXME: Page "EN: Shortcut to welcome" must to be rendered in menu, needs a refactored menu generation
-                    ['title' => 'EN: Shortcut to welcome', 'link' => ''],
+                    ['title' => 'EN: Shortcut to welcome', 'link' => '/de-ch/hello'],
                 ],
             ],
             [
@@ -161,8 +160,7 @@ class ScenarioETest extends AbstractLocalizedPagesTestCase
                 'menu' => [
                     ['title' => 'DE: Ãœber uns', 'link' => '/de-ch/ueber-uns'],
                     ['title' => 'DE: Produkte', 'link' => '/de-ch/produkte'],
-                    // FIXME: Page "EN: Shortcut to welcome" must to be rendered in menu, needs a refactored menu generation
-                    ['title' => 'EN: Shortcut to welcome', 'link' => ''],
+                    ['title' => 'EN: Shortcut to welcome', 'link' => '/de-ch/hello'],
                 ],
             ],
         ];
-- 
GitLab