[FEATURE] Add hook to add custom TypoScript templates 06/51106/5
authorBenni Mack <benni@typo3.org>
Tue, 3 Jan 2017 12:26:19 +0000 (13:26 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Wed, 11 Jan 2017 17:37:31 +0000 (18:37 +0100)
This hook allows to extend the added TypoScript by adding
custom TypoScript templates (e.g. not loaded from the database)

Resolves: #79140
Releases: master
Change-Id: Ie2350c809685fdc3886f5f77761bad5124baa2bb
Reviewed-on: https://review.typo3.org/51106
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Classes/TypoScript/TemplateService.php
typo3/sysext/core/Documentation/Changelog/master/Feature-79140-AddHookToAddCustomTypoScriptTemplates.rst [new file with mode: 0644]

index adb5cd5..1e99a47 100644 (file)
@@ -660,6 +660,20 @@ class TemplateService
             $this->rootLine[] = $this->absoluteRootLine[$a];
         }
 
+        // Hook into the default TypoScript to add custom typoscript logic
+        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Core/TypoScript/TemplateService']['runThroughTemplatesPostProcessing'])) {
+            $hookParameters = [
+                'extensionStaticsProcessed' => &$this->extensionStaticsProcessed,
+                'isDefaultTypoScriptAdded'  => &$this->isDefaultTypoScriptAdded,
+                'absoluteRootLine' => &$this->absoluteRootLine,
+                'rootLine'         => &$this->rootLine,
+                'startTemplateUid' => $start_template_uid,
+            ];
+            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Core/TypoScript/TemplateService']['runThroughTemplatesPostProcessing'] as $listener) {
+                GeneralUtility::callUserFunction($listener, $hookParameters, $this);
+            }
+        }
+
         // Process extension static files if not done yet, but explicitly requested
         if (!$this->extensionStaticsProcessed && $this->processExtensionStatics) {
             $this->addExtensionStatics('sys_0', 'sys_0', 0, []);
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-79140-AddHookToAddCustomTypoScriptTemplates.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-79140-AddHookToAddCustomTypoScriptTemplates.rst
new file mode 100644 (file)
index 0000000..414b8ba
--- /dev/null
@@ -0,0 +1,65 @@
+.. include:: ../../Includes.txt
+
+=============================================================
+Feature: #79140 - Add hook to add custom TypoScript templates
+=============================================================
+
+See :issue:`79140`
+
+Description
+===========
+
+A new hook in TemplateService allows to add or modify existing TypoScript templates.
+
+Register the hook via :php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Core/TypoScript/TemplateService']['runThroughTemplatesPostProcessing']`
+in the extensions' ext_localconf.php file.
+
+Example
+=======
+
+An example implementation could look like this:
+
+EXT:my_site/ext_localconf.php
+
+.. code-block:: php
+
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Core/TypoScript/TemplateService']['runThroughTemplatesPostProcessing'][1313131313] =
+   \MyVendor\MySite\Hooks\TypoScriptHook::class . '->addCustomTypoScriptTemplate';
+
+
+EXT:my_site/Classes/Hooks/TypoScriptHook.php
+
+.. code-block:: php
+
+namespace MyVendor\MySite\Hooks;
+
+class TypoScriptHook
+{
+
+   /**
+    * Hooks into TemplateService after
+    * @param array $parameters
+    * @param \TYPO3\CMS\Core\TypoScript\TemplateService $parentObject
+    * @return void
+    */
+   public function addCustomTypoScriptTemplate($parameters, $parentObject)
+   {
+      // Disable the inclusion of default TypoScript set via TYPO3_CONF_VARS
+      $parameters['isDefaultTypoScriptAdded'] = true;
+      // Disable the inclusion of ext_typoscript_setup.txt of all extensions
+      $parameters['processExtensionStatics'] = false;
+
+      // No template was found in rootline so far, so a custom "fake" sys_template record is added
+      if ($parentObject->outermostRootlineIndexWithTemplate === 0) {
+         $row = [
+            'uid' => 'my_site_template',
+            'config' => '<INCLUDE_TYPOSCRIPT: source="FILE:EXT:my_site/Configuration/TypoScript/site_setup.t3s">',
+            'root' => 1,
+            'pid' => 0
+         ];
+         $parentObject->processTemplate($row, 'sys_' . $row['uid'], 0, 'sys_' . $row['uid']);
+      }
+   }
+}
+
+.. index:: PHP-API, TypoScript