From ed765fb484ec8860b5b7c24e04f4bf4baff8fad6 Mon Sep 17 00:00:00 2001
From: Markus Klein <markus.klein@typo3.org>
Date: Tue, 17 Nov 2015 20:52:09 +0100
Subject: [PATCH] [BUGFIX] Handle pageTSconfig correctly for TS

The page TSconfig includes need to be processed for TypoScript
as well in order to have constants from TSconfig available.

Resolves: #71640
Releases: master
Change-Id: I8dcbfa4498621567e8cb3caac6235031af2c70c1
Reviewed-on: https://review.typo3.org/44764
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
---
 .../Classes/TypoScript/TemplateService.php    | 39 +++++++++++++++++++
 .../Utility/ExtensionManagementUtility.php    | 22 +++++++----
 .../core/Classes/Utility/RootlineUtility.php  |  1 +
 3 files changed, 55 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/core/Classes/TypoScript/TemplateService.php b/typo3/sysext/core/Classes/TypoScript/TemplateService.php
index 974849c0a4e2..84dcde1ef2bc 100644
--- a/typo3/sysext/core/Classes/TypoScript/TemplateService.php
+++ b/typo3/sysext/core/Classes/TypoScript/TemplateService.php
@@ -1123,6 +1123,12 @@ class TemplateService
         // Setting default configuration:
         $TSdataArray[] = $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPageTSconfig'];
         for ($a = 0; $a <= $this->outermostRootlineIndexWithTemplate; $a++) {
+            if (trim($this->absoluteRootLine[$a]['tsconfig_includes'])) {
+                $includeTsConfigFileList = GeneralUtility::trimExplode(',',
+                    $this->absoluteRootLine[$a]['tsconfig_includes'], true);
+
+                $TSdataArray = $this->mergeConstantsFromIncludedTsConfigFiles($includeTsConfigFileList, $TSdataArray);
+            }
             $TSdataArray[] = $this->absoluteRootLine[$a]['TSconfig'];
         }
         // Parsing the user TS (or getting from cache)
@@ -1134,9 +1140,42 @@ class TemplateService
         if (is_array($parseObj->setup['TSFE.']['constants.'])) {
             ArrayUtility::mergeRecursiveWithOverrule($constArray, $parseObj->setup['TSFE.']['constants.']);
         }
+
         return $constArray;
     }
 
+    /**
+     * Reads TSconfig defined in external files and appends it to the given TSconfig array (in this case only constants)
+     *
+     * @param array $filesToInclude The files to read constants from
+     * @param array $TSdataArray The TSconfig array the constants should be appended to
+     * @return array The TSconfig with the included constants appended
+     */
+    protected function mergeConstantsFromIncludedTsConfigFiles($filesToInclude, $TSdataArray)
+    {
+        foreach ($filesToInclude as $key => $file) {
+            if (!StringUtility::beginsWith($file, 'EXT:')) {
+                continue;
+            }
+
+            list($extensionKey, $filePath) = explode('/', substr($file, 4), 2);
+
+            if ((string)$extensionKey === '' || !ExtensionManagementUtility::isLoaded($extensionKey)) {
+                continue;
+            }
+            if ((string)$filePath == '') {
+                continue;
+            }
+
+            $tsConfigFile = ExtensionManagementUtility::extPath($extensionKey) . $filePath;
+            if (file_exists($tsConfigFile)) {
+                $TSdataArray[] = GeneralUtility::getUrl($tsConfigFile);
+            }
+        }
+
+        return $TSdataArray;
+    }
+
     /**
      * This flattens a hierarchical TypoScript array to $this->flatSetup
      *
diff --git a/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php b/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
index 7479567a552b..4a7147cfbc5e 100644
--- a/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
+++ b/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
@@ -1476,20 +1476,28 @@ tt_content.' . $key . $suffix . ' {
 
     /**
      * Call this method to add an entry in the pageTSconfig list found in pages
-     * FOR USE in ext_tables.php FILES or files in Configuration/TCA/Overrides/*.php Use the latter to benefit from TCA caching!
+     * FOR USE in files in Configuration/TCA/Overrides/*.php Use the latter to benefit from TCA caching!
      *
      * @param string $extKey The extension key
-     * @param string $file The path and title where the TSconfig file is located
+     * @param string $filePath The path where the TSconfig file is located
      * @param string $title The title in the selector box
      * @return void
      */
-    public static function registerPageTSConfigFile($extKey, $file, $title)
+    public static function registerPageTSConfigFile($extKey, $filePath, $title)
     {
-        if ($extKey && $file && is_array($GLOBALS['TCA']['pages']['columns'])) {
-            $value = str_replace(',',  '', 'EXT:' . $extKey . '/' . $file);
-            $itemArray = array(trim($title . ' (' . $extKey . ')'), $value);
-            $GLOBALS['TCA']['pages']['columns']['tsconfig_includes']['config']['items'][] = $itemArray;
+        if (!$extKey) {
+            throw new \InvalidArgumentException('No extension key given.', 1447789490);
+        }
+        if (!$filePath) {
+            throw new \InvalidArgumentException('No file path given.', 1447789491);
         }
+        if (!isset($GLOBALS['TCA']['pages']['columns']) || !is_array($GLOBALS['TCA']['pages']['columns'])) {
+            throw new \InvalidArgumentException('No TCA definition for table "pages".', 1447789492);
+        }
+
+        $value = str_replace(',',  '', 'EXT:' . $extKey . '/' . $filePath);
+        $itemArray = array(trim($title . ' (' . $extKey . ')'), $value);
+        $GLOBALS['TCA']['pages']['columns']['tsconfig_includes']['config']['items'][] = $itemArray;
     }
 
     /**
diff --git a/typo3/sysext/core/Classes/Utility/RootlineUtility.php b/typo3/sysext/core/Classes/Utility/RootlineUtility.php
index 55d9ef8b3498..cb58333e0cc5 100644
--- a/typo3/sysext/core/Classes/Utility/RootlineUtility.php
+++ b/typo3/sysext/core/Classes/Utility/RootlineUtility.php
@@ -84,6 +84,7 @@ class RootlineUtility
         'extendToSubpages',
         'doktype',
         'TSconfig',
+        'tsconfig_includes',
         'is_siteroot',
         'mount_pid',
         'mount_pid_ol',
-- 
GitLab