[FEATURE] Automatically determine page type for URIs with format 23/21123/3
authorLienhart Woitok <lienhart.woitok@netlogix.de>
Sat, 18 Jun 2011 17:58:58 +0000 (19:58 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 2 Jun 2013 09:21:35 +0000 (11:21 +0200)
When rendering a special format a custom page type is required.
This adds a mapping configuration to define which page type
should be used for which format.

Example:
plugin.tx_myext {
    view.formatToPageTypeMapping {
        txt = 99
        pdf = 123
    }
}

The formats defined will apply to all Plugins in your extension.

Change-Id: I22beb6334a094711abfb2ee7b53fb3e065ec8580
Releases: 6.2
Resolves: #27498
Reviewed-on: https://review.typo3.org/21123
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Mvc/Web/Routing/UriBuilder.php
typo3/sysext/extbase/Classes/Service/ExtensionService.php
typo3/sysext/extbase/Tests/Unit/Mvc/Web/Routing/UriBuilderTest.php

index 928e39c..a5efd98 100644 (file)
@@ -388,7 +388,6 @@ class UriBuilder {
 
        /**
         * @return integer
-        * @api
         */
        public function getTargetPageType() {
                return $this->targetPageType;
@@ -623,6 +622,9 @@ class UriBuilder {
                $typolinkConfiguration['parameter'] = $this->targetPageUid !== NULL ? $this->targetPageUid : $GLOBALS['TSFE']->id;
                if ($this->targetPageType !== 0) {
                        $typolinkConfiguration['parameter'] .= ',' . $this->targetPageType;
+               } elseif ($this->format !== '') {
+                       $targetPageType = $this->extensionService->getTargetPageTypeByFormat($this->request->getControllerExtensionKey(), $this->format);
+                       $typolinkConfiguration['parameter'] .= ',' . $targetPageType;
                }
                if (count($this->arguments) > 0) {
                        $arguments = $this->convertDomainObjectsToIdentityArrays($this->arguments);
@@ -707,6 +709,7 @@ class UriBuilder {
                }
                return $result;
        }
+
 }
 
 ?>
\ No newline at end of file
index def054d..4837adf 100644 (file)
@@ -209,6 +209,24 @@ class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface {
                $actions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'];
                return current($actions);
        }
+
+       /**
+        * Resolve the page type number to use for building a link for a specific format
+        *
+        * @param string $extensionName name of the extension that has defined the target page type
+        * @param string $format The format for which to look up the page type
+        * @return integer Page type number for target page
+        */
+       public function getTargetPageTypeByFormat($extensionName, $format) {
+               $targetPageType = 0;
+               $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, $extensionName);
+               $formatToPageTypeMapping = isset($settings['view']['formatToPageTypeMapping']) ? $settings['view']['formatToPageTypeMapping'] : array();
+               if (is_array($formatToPageTypeMapping) && array_key_exists($format, $formatToPageTypeMapping)) {
+                       $targetPageType = (integer) $formatToPageTypeMapping[$format];
+               }
+               return $targetPageType;
+       }
+
 }
 
 ?>
\ No newline at end of file
index 7bed74e..3cafd26 100644 (file)
@@ -475,9 +475,19 @@ class UriBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        /**
         * @test
         */
-       public function buildTypolinkConfigurationConsidersPageType() {
+       public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
                $this->uriBuilder->setTargetPageUid(123);
-               $this->uriBuilder->setTargetPageType(2);
+               $this->uriBuilder->setFormat('txt');
+
+               $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
+               $mockConfigurationManager->expects($this->any())->method('getConfiguration')
+                       ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
+               $this->uriBuilder->injectConfigurationManager($mockConfigurationManager);
+
+               $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
+                       ->with(NULL, 'txt')
+                       ->will($this->returnValue(2));
+
                $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
                $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
                $this->assertEquals($expectedConfiguration, $actualConfiguration);
@@ -486,6 +496,50 @@ class UriBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        /**
         * @test
         */
+       public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setFormat('txt');
+
+               $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
+               $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
+               $this->uriBuilder->injectConfigurationManager($mockConfigurationManager);
+
+               $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
+                       ->with(NULL, 'txt')
+                       ->will($this->returnValue(0));
+
+               $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
+
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
+       }
+
+       /**
+        * @test
+        */
+       public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setFormat('txt');
+
+               $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
+               $mockConfigurationManager->expects($this->any())->method('getConfiguration')
+                       ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
+               $this->uriBuilder->injectConfigurationManager($mockConfigurationManager);
+
+               $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
+                       ->with(NULL, 'txt')
+                       ->will($this->returnValue(0));
+
+               $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
+
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
+       }
+
+
+       /**
+        * @test
+        */
        public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
                $this->uriBuilder->setTargetPageUid(123);
                $this->uriBuilder->setNoCache(TRUE);