[FEATURE] Add Data Processing to FLUIDTEMPLATE content object 19/39519/7
authorPatrick Broens <patrick@patrickbroens.nl>
Wed, 13 May 2015 07:57:26 +0000 (09:57 +0200)
committerBenjamin Mack <benni@typo3.org>
Thu, 21 May 2015 13:43:55 +0000 (15:43 +0200)
The content object FLUIDTEMPLATE is extended with `dataProcessing`. With this
setting you can add full namespace class references, which can manipulate
the $data variable of the current rendered content object, like tt_content or
page, before assigning this variable to the view.

Change-Id: I757db37d86e1cf907de0116c3c63effc5270a3c1
Resolves: #66907
Releases: master
Reviewed-on: http://review.typo3.org/39519
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Benjamin Mack <benni@typo3.org>
Tested-by: Benjamin Mack <benni@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-66907-AddDataProcessingToFluidTemplateContentObject.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/FluidTemplateContentObject.php
typo3/sysext/frontend/Classes/ContentObject/FluidTemplateDataProcessorInterface.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-66907-AddDataProcessingToFluidTemplateContentObject.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-66907-AddDataProcessingToFluidTemplateContentObject.rst
new file mode 100644 (file)
index 0000000..e69ffe6
--- /dev/null
@@ -0,0 +1,43 @@
+=====================================================================
+Feature: #66907 - Add Data Processing to FLUIDTEMPLATE content object
+=====================================================================
+
+Description
+===========
+
+cObject FLUIDTEMPLATE is extended with ``dataProcessing``. This setting can be used to add one or multiple processors to
+manipulate the ``$data`` variable of the currently rendered content object, like tt_content or page.
+
+- dataProcessing = array of class references by full namespace
+
+
+Example:
+--------
+
+.. code-block:: typoscript
+
+       my_custom_ctype = FLUIDTEMPLATE
+       my_custom_ctype {
+               templateRootPaths {
+                       10 = EXT:your_extension_key/Resources/Private/Templates
+               }
+               templateName = CustomName
+               settings {
+                       extraParam = 1
+               }
+               dataProcessing {
+                       1 = Vendor\YourExtensionKey\DataProcessing\MyFirstCustomProcessor
+                       2 = Vendor2\AnotherExtensionKey\DataProcessing\MySecondCustomProcessor
+                       2 {
+                               options {
+                                       myOption = SomeValue
+                               }
+                       }
+               }
+       }
+
+
+Impact
+======
+
+The data processors can be used in all new projects. There is no interference with any part of existing code.
\ No newline at end of file
index ac16243..eb1409c 100644 (file)
@@ -37,6 +37,7 @@ class FluidTemplateContentObject extends AbstractContentObject {
         * - layoutRootPaths array of filepath+stdWrap Root paths to layouts (fallback)
         * - partialRootPaths array of filepath+stdWrap Root paths to partials (fallback)
         * - variable array of cObjects, the keys are the variable names in fluid
+        * - dataProcessing array of data processors which are classes to manipulate $data
         * - extbase.pluginName
         * - extbase.controllerExtensionName
         * - extbase.controllerName
@@ -71,6 +72,7 @@ class FluidTemplateContentObject extends AbstractContentObject {
                $this->setExtbaseVariables($conf);
                $this->assignSettings($conf);
                $this->assignContentObjectVariables($conf);
+               $this->processData($conf);
                $this->assignContentObjectDataAndCurrent();
 
                $content = $this->renderFluidView();
@@ -213,6 +215,45 @@ class FluidTemplateContentObject extends AbstractContentObject {
        }
 
        /**
+        * Check for the availability of processors, defined in TypoScript, and use them for data processing
+        *
+        * @param array $configuration Configuration array
+        * @return void
+        * @throws \UnexpectedValueException
+        */
+       protected function processData(array $configuration) {
+               if (
+                       !empty($configuration['dataProcessing.'])
+                       && is_array($configuration['dataProcessing.'])
+               ) {
+                       $processors = $configuration['dataProcessing.'];
+
+                       foreach ($processors as $key => $className) {
+                               if (strpos($key, '.') === FALSE && !empty($className)) {
+                                       $processor = GeneralUtility::makeInstance($className);
+
+                                       if (!$processor instanceof FluidTemplateDataProcessorInterface) {
+                                               throw new \UnexpectedValueException(
+                                                       '$processor with class name "' . $className . '" ' .
+                                                       'must implement interface "' . FluidTemplateDataProcessorInterface::class . '"',
+                                                       1427455377
+                                               );
+                                       }
+
+                                       $processorConfiguration = isset($processors[$key . '.']) ? $processors[$key . '.'] : array();
+
+                                       $processor->process(
+                                               $this->cObj->data,
+                                               $processorConfiguration,
+                                               $configuration,
+                                               $this->view
+                                       );
+                               }
+                       }
+               }
+       }
+
+       /**
         * Assign rendered content objects in variables array to view
         *
         * @param array $conf Configuration array
diff --git a/typo3/sysext/frontend/Classes/ContentObject/FluidTemplateDataProcessorInterface.php b/typo3/sysext/frontend/Classes/ContentObject/FluidTemplateDataProcessorInterface.php
new file mode 100644 (file)
index 0000000..738fa63
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+namespace TYPO3\CMS\Frontend\ContentObject;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Fluid\View\StandaloneView;
+
+/**
+ * Interface for data processor classes with the FLUIDTEMPLATE content object
+ */
+interface FluidTemplateDataProcessorInterface {
+
+       /**
+        * Process data passed to the FLUIDTEMPLATE content object
+        *
+        * @param array $data The data of the content element or page
+        * @param array $processorConfiguration The configuration of this processor
+        * @param array $configuration The configuration of FLUIDTEMPLATE
+        * @param \TYPO3\CMS\Fluid\View\StandaloneView $view The view
+        * @return void
+        */
+       public function process(
+               array &$data,
+               array $processorConfiguration,
+               array $configuration,
+               StandaloneView $view
+       );
+}