From c03dc58ff948abae2f45827352262146a325df6d Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Thu, 16 Jul 2020 17:14:01 +0200
Subject: [PATCH] [!!!][BUGFIX] Set up language in FE before TypoScript is
 evaluated

This change moves the language evaluation of a page record much earlier
in the frontend process.

Before:
* Resolve the page (determineId) based on permissions, visibility etc.
* get TypoScript from Cache
* Find a translation of the page and determine the content language

Now:
* Resolve the page based on permissions, visibility etc
* Find the translation of the page
* Get TypoScript from Cache

This is a breaking change, as the hooks within settingLanguage() are now
called at a different time of the processing logic for a frontend request,
however it solves an issue that you can now use localized values for
TypoScript conditions.

Resolves: #23736
Releases: master
Change-Id: Ia2db0233ce6680ae1cbcddb81574a41fa6333abf
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65044
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 ...tionSetEarlierInFrontendRequestProcess.rst | 56 +++++++++++++++++++
 .../TypoScriptFrontendController.php          |  3 +-
 .../PrepareTypoScriptFrontendRendering.php    |  5 --
 .../Classes/Service/RedirectService.php       |  2 +-
 4 files changed, 59 insertions(+), 7 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Breaking-23736-PageLanguageDetectionSetEarlierInFrontendRequestProcess.rst

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-23736-PageLanguageDetectionSetEarlierInFrontendRequestProcess.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-23736-PageLanguageDetectionSetEarlierInFrontendRequestProcess.rst
new file mode 100644
index 000000000000..425a1d38fd4d
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Breaking-23736-PageLanguageDetectionSetEarlierInFrontendRequestProcess.rst
@@ -0,0 +1,56 @@
+.. include:: ../../Includes.txt
+
+==================================================================================
+Breaking: #23736 - Page Language detection set earlier in Frontend Request Process
+==================================================================================
+
+See :issue:`23736`
+
+Description
+===========
+
+TYPO3's Frontend Request Process previously needed a parsed TypoScript before doing the language overlay of the currently
+visited page, as previous TYPO3 sites without Site Handling
+used TypoScript conditions like `[globalVar = GP:L = 1]` to switch
+between languages.
+
+This made it impossible to use conditions for accessing the translated page record like `[page["nav_title"] == "Bienvenue"]`,
+which was a long outstanding conceptual issue that was finally
+made possible through Site Handling.
+
+For this reason, the translated page is now resolved directly after the actual page and rootline resolving.
+
+
+Impact
+======
+
+The translated page record (based on the fallback handling in the
+Site Configuration) is now available in :php:`$TSFE->page` at a much earlier stage of the Frontend process.
+
+This means, TypoScript conditions based on the page record (see example above) might be different.
+
+In addition, the two hooks 
+:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLanguage_preProcess']` and
+:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLanguage_postProcess']` are
+called now earlier when no TypoScript is available yet.
+
+
+Affected Installations
+======================
+
+TYPO3 installations with custom extensions using the hooks mentioned
+above or that have language-specific "page-based" conditions.
+
+
+Migration
+=========
+
+Review the hooks or use a PSR-15 middleware to use the same place
+to extend TYPO3's Frontend Request process after TypoScript was
+initialized.
+
+Also, be sure to review any of the TypoScript conditions (possible
+via the `Web->Template` module) if they are related to values only
+available in the default language, which seems to be a very case however.
+
+.. index:: PHP-API, TypoScript, NotScanned, ext:frontend
\ No newline at end of file
diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 284dbe19a732..b5fa1faa7710 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -793,6 +793,8 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         $this->id = ($this->contentPid = (int)$this->id);
         // Make sure it's an integer
         $this->type = (int)$this->type;
+        // Setting language and fetch translated page
+        $this->settingLanguage($request);
         // Call post processing function for id determination:
         $_params = ['pObj' => &$this];
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc'] ?? [] as $_funcRef) {
@@ -2027,7 +2029,6 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         } catch (RootLineException $e) {
             $this->rootLine = [];
         }
-        $this->tmpl->updateRootlineData($this->rootLine);
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
index 17781405db87..c468bb54af90 100644
--- a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
+++ b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
@@ -70,11 +70,6 @@ class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
         // After this, we should have a valid config-array ready
         $this->controller->getConfigArray($request);
 
-        // Setting language and locale
-        $this->timeTracker->push('Setting language');
-        $this->controller->settingLanguage($request);
-        $this->timeTracker->pull();
-
         // Convert POST data to utf-8 for internal processing if metaCharset is different
         if ($this->controller->metaCharset !== 'utf-8' && $request->getMethod() === 'POST') {
             $parsedBody = $request->getParsedBody();
diff --git a/typo3/sysext/redirects/Classes/Service/RedirectService.php b/typo3/sysext/redirects/Classes/Service/RedirectService.php
index 6805b42abd23..52a189158b42 100644
--- a/typo3/sysext/redirects/Classes/Service/RedirectService.php
+++ b/typo3/sysext/redirects/Classes/Service/RedirectService.php
@@ -334,9 +334,9 @@ class RedirectService implements LoggerAwareInterface
             $frontendUserAuthentication
         );
         $controller->fetch_the_id();
+        $controller->settingLanguage();
         $controller->calculateLinkVars($queryParams);
         $controller->getConfigArray();
-        $controller->settingLanguage();
         $controller->newCObj();
         if (!$GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
             $GLOBALS['TSFE'] = $controller;
-- 
GitLab