[FEATURE] FLUIDTEMPLATE cObj settings array support 49/17849/4
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 30 Jan 2013 16:43:13 +0000 (17:43 +0100)
committerJigal van Hemert <jigal@xs4all.nl>
Wed, 30 Jan 2013 17:35:16 +0000 (18:35 +0100)
extbase action controllers give a settings typoscript sub array
to the view, if it exists. This feature is very handy, it allows
full typoscript magic like constants access on this sub array.

The FLUIDTEMPLATE content object has a the variables typoscript
configuration, but this allows only other content objects in it. This
is unpractical if just some key-value nested array should be given
to the view.

The patch adds a settings property to the FLUIDTEMPLATE cObj that
behaves similar to the extbase action controller. If given, this
array is just assigned as is to the view.

Example:

lib.globalSettings {
# Access to constants works, too.
foo = value
}
page = PAGE
page.10 = FLUIDTEMPLATE
page.10 {
file = fileadmin/templates/MyTemplate.html
settings < lib.globalSettings
}

In the view, the value can then be used: {settings.foo}

Change-Id: Ie48911275832d422525e7d8891096fefcc0af152
Resolves: #23853
Releases: 6.1
Reviewed-on: https://review.typo3.org/17849
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
typo3/sysext/frontend/Classes/ContentObject/FluidTemplateContentObject.php
typo3/sysext/frontend/Tests/Unit/ContentObject/FluidTemplateContentObjectTest.php

index b907205..bccdd6a 100644 (file)
@@ -89,6 +89,7 @@ class FluidTemplateContentObject extends \TYPO3\CMS\Frontend\ContentObject\Abstr
                $this->setPartialRootPath($conf);
                $this->setFormat($conf);
                $this->setExtbaseVariables($conf);
+               $this->assignSettings($conf);
                $this->assignContentObjectVariables($conf);
                $this->assignContentObjectDataAndCurrent($conf);
 
@@ -229,6 +230,22 @@ class FluidTemplateContentObject extends \TYPO3\CMS\Frontend\ContentObject\Abstr
        }
 
        /**
+        * Set any TypoScript settings to the view. This is similar to a
+        * default MVC action controller in extbase.
+        *
+        * @param array $conf Configuration
+        * @return void
+        */
+       protected function assignSettings(array $conf) {
+               if (array_key_exists('settings.', $conf)) {
+                       /** @var $typoScriptService \TYPO3\CMS\Extbase\Service\TypoScriptService */
+                       $typoScriptService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
+                       $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
+                       $this->view->assign('settings', $settings);
+               }
+       }
+
+       /**
         * Assign content object renderer data and current to view
         *
         * @param array $conf Configuration
@@ -263,4 +280,4 @@ class FluidTemplateContentObject extends \TYPO3\CMS\Frontend\ContentObject\Abstr
        }
 
 }
-?>
+?>
\ No newline at end of file
index 8f1e3ed..4a413b3 100644 (file)
@@ -31,6 +31,11 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
 class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
+        * @var array A backup of registered singleton instances
+        */
+       protected $singletonInstances = array();
+
+       /**
         * @var \TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
         */
        protected $fixture = NULL;
@@ -54,6 +59,7 @@ class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         * Set up
         */
        public function setUp() {
+               $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
                $this->contentObjectRenderer = $this->getMock(
                        'TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer'
                );
@@ -69,6 +75,13 @@ class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
        }
 
        /**
+        * Tear down
+        */
+       public function tearDown() {
+               \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
+       }
+
+       /**
         * Add a mock standalone view to fixture
         */
        protected function addMockViewToFixture() {
@@ -391,6 +404,44 @@ class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 
        /**
         * @test
+        */
+       public function renderAssignsSettingsArrayToView() {
+               $this->addMockViewToFixture();
+
+               $configuration = array(
+                       'settings.' => array(
+                               'foo' => 'value',
+                               'bar.' => array(
+                                       'baz' => 'value2',
+                               ),
+                       ),
+               );
+
+               $expectedSettingsToBeSet = array(
+                       'foo' => 'value',
+                       'bar' => array(
+                               'baz' => 'value2',
+                       ),
+               );
+
+               $typoScriptServiceMock = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
+               $typoScriptServiceMock
+                       ->expects($this->once())
+                       ->method('convertTypoScriptArrayToPlainArray')
+                       ->with($configuration['settings.'])
+                       ->will($this->returnValue($expectedSettingsToBeSet));
+               \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService', $typoScriptServiceMock);
+
+               $this->standaloneView
+                       ->expects($this->at(1))
+                       ->method('assign')
+                       ->with('settings', $expectedSettingsToBeSet);
+
+               $this->fixture->render($configuration);
+       }
+
+       /**
+        * @test
         * @expectedException \InvalidArgumentException
         */
        public function renderThrowsExceptionForNotAllowedVariableData() {
@@ -526,4 +577,4 @@ class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
                $this->fixture->render($configuration);
        }
 }
-?>
+?>
\ No newline at end of file