[FEATURE] Integrate Uri.TypolinkViewHelper 06/34106/10
authorJan Helke <typo3@helke.de>
Thu, 13 Nov 2014 19:00:51 +0000 (20:00 +0100)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Sun, 21 Dec 2014 20:32:47 +0000 (21:32 +0100)
Based on the existing typolink view helper.
This is a variant to just return the URI.

Resolves: #62931
Releases: master
Change-Id: Iedf7972917a0b5ee1b7b25112cbe9238026236f7
Reviewed-on: http://review.typo3.org/34106
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/core/Documentation/Changelog/7.0/Feature-59396-TypolinkViewHelper.rst
typo3/sysext/fluid/Classes/ViewHelpers/Uri/TypolinkViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Uri/TypolinkViewHelperTest.php [new file with mode: 0644]

index 4d5b029..17d3634 100644 (file)
@@ -5,7 +5,7 @@ Feature: #59396 - TypolinkViewHelper
 Description
 ===========
 
-Added a ViewHelper that deals with the contents of any field that was filled with a link wizard in
+Adding Link and Uri ViewHelper that cope with the contents of any field that was filled with a link wizard in
 TYPO3 CMS Backend.
 Those fields contain various parts split by a space and being escaped to provide input for the
 typoLink function.
@@ -17,7 +17,9 @@ The full parameter usage in Fluid might look like this, where {link} is the fiel
 
 ::
 
-<f:link.typolink parameter="{link}" target="_blank" class="ico-class" title="some title" additionalParams="" additionalAttributes="{type:'button'}">
+<f:link.typolink parameter="{link}" target="_blank" class="ico-class" title="some title" additionalParams="b=u" additionalAttributes="{type:'button'}">
+
+<f:uri.typolink parameter="{link}" additionalParameters="b=u">
 
 ..
 
@@ -31,11 +33,15 @@ While passing additional parameters to the ViewHelper, following rules apply:
 - additionalAttributes is (as usual) added to the resulting tag as *type="button"*
 
 {link} contains *19 _blank - "testtitle with whitespace" &X=y*.
+In case of the Uri.Typolink Viewhelper, only the first and the fourth parameter of the field content are taken into account,
+the tag related properties are discarded.
 For the given example, the output is:
 
 ::
 
-<a href="index.php?id=19&X=y" title="some title" target="_blank" class="ico-class" type="button">
+<a href="index.php?id=19&X=y&b=u" title="some title" target="_blank" class="ico-class" type="button">
+
+index.php?id=19&X=y&b=u
 
 ..
 
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Uri/TypolinkViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Uri/TypolinkViewHelper.php
new file mode 100644 (file)
index 0000000..8135142
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+
+/**
+ * A ViewHelper to create uris from fields supported by the link wizard
+ *
+ * == Example ==
+ *
+ * {link} contains "19 - - - &X=y"
+ * Please note that due to the nature of typolink you have to provide a
+ * full set of parameters if you use the parameter only. Target, class
+ * and title will be discarded.
+ *
+ * <code title="minimal usage">
+ * <f:uri.typolink parameter="{link}" />
+ * <output>
+ * index.php?id=19&X=y
+ * </output>
+ * </code>
+ *
+ * <code title="Full parameter usage">
+ * <f:uri.typolink parameter="{link}" additionalParams="&u=b" />
+ * </code>
+ * <output>
+ * index.php?id=19&X=y&u=b
+ * </output>
+ *
+ */
+class TypolinkViewHelper extends AbstractViewHelper {
+
+       /**
+        * Render
+        *
+        * @param string $parameter stdWrap.typolink style parameter string
+        * @param string $additionalParams
+        *
+        * @return string
+        */
+       public function render($parameter, $additionalParams = '') {
+               // Merge the $parameter with other arguments
+               $typolinkParameter = $this->createTypolinkParameterArrayFromArguments($parameter, $additionalParams);
+
+               $content = '';
+
+               if ($parameter) {
+                       $contentObject = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
+                       $content = $contentObject->typoLink_URL(
+                               array(
+                                       'parameter' => implode(' ', $typolinkParameter),
+                               )
+                       );
+               }
+
+               return $content;
+       }
+
+       /**
+        * Transforms ViewHelper arguments to typo3link.parameters.typoscript option as array.
+        *
+        * @param string $parameter Example: 19 _blank - "testtitle with whitespace" &X=y
+        * @param string $additionalParameters
+        *
+        * @return array Final merged typolink.parameter as array to be imploded with empty string later
+        */
+       protected function createTypolinkParameterArrayFromArguments($parameter, $additionalParameters = '') {
+               // Explode $parameter by whitespace and remove any " around resulting array values
+               $parameterArray = GeneralUtility::unQuoteFilenames($parameter, TRUE);
+
+               if (empty($parameterArray)) {
+                       return array();
+               }
+
+               // Extend to 4 elements
+               $typolinkConfiguration = array_pad($parameterArray, 4, '-');
+
+               // Combine additionalParameters
+               if ($additionalParameters) {
+                       $typolinkConfiguration[4] .= $additionalParameters;
+               }
+
+               // Unset unused parameters again from the end, wrap all given values with "
+               $reverseSortedParameters = array_reverse($typolinkConfiguration, TRUE);
+               $aValueWasSet = FALSE;
+               foreach ($reverseSortedParameters as $position => $value) {
+                       if ($value === '-' && !$aValueWasSet) {
+                               unset($typolinkConfiguration[$position]);
+                       } else {
+                               $aValueWasSet = TRUE;
+                               if ($value !== '-') {
+                                       $typolinkConfiguration[$position] = '"' . $value . '"';
+                               }
+                       }
+               }
+               return $typolinkConfiguration;
+       }
+}
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Uri/TypolinkViewHelperTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Uri/TypolinkViewHelperTest.php
new file mode 100644 (file)
index 0000000..a29867c
--- /dev/null
@@ -0,0 +1,143 @@
+<?php
+namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Uri;
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Class TypolinkViewHelperTest
+ */
+class TypolinkViewHelperTest extends ViewHelperBaseTestcase {
+
+       /**
+        * @return array
+        */
+       public function typoScriptConfigurationData() {
+               return array(
+                       'empty input' => array(
+                               '', // input from link field
+                               '', // additional parameters from fluid
+                               array(), //expected typolink Array
+                '' // expected URI
+                       ),
+                       'simple id input' => array(
+                               19,
+                               '',
+                               array(
+                                       0 => '"19"',
+                               ),
+                'index.php?id=19'
+                       ),
+                       'external url with target' => array(
+                               'www.web.de _blank',
+                               '',
+                               array(
+                                       0 => '"www.web.de"',
+                                       1 => '"_blank"',
+                               ),
+                'http://www.web.de'
+                       ),
+                       'page with class' => array(
+                               '42 - css-class',
+                               '',
+                               array(
+                                       0 => '"42"',
+                                       1 => '-',
+                                       2 => '"css-class"',
+                               ),
+                "index.php?id=42"
+                       ),
+                       'page with title' => array(
+                               '42 - - "a link title"',
+                               '',
+                               array(
+                                       0 => '"42"',
+                                       1 => '-',
+                                       2 => '-',
+                                       3 => '"a link title"'
+                               ),
+                "index.php?id=42"
+                       ),
+                       'page with title and parameters' => array(
+                               '42 - - "a link title" &x=y',
+                               '',
+                               array(
+                                       0 => '"42"',
+                                       1 => '-',
+                                       2 => '-',
+                                       3 => '"a link title"',
+                                       4 => '"&x=y"',
+                               ),
+                "index.php?id=42&x=y"
+                       ),
+                       'page with title and extended parameters' => array(
+                               '42 - - "a link title" &x=y',
+                               '&a=b',
+                               array(
+                                       0 => '"42"',
+                                       1 => '-',
+                                       2 => '-',
+                                       3 => '"a link title"',
+                                       4 => '"&x=y&a=b"',
+                               ),
+                "index.php?id=42&x=y&a=b"
+                       ),
+                       'full parameter usage' => array(
+                               '19 _blank css-class "testtitle with whitespace" &X=y',
+                               '&a=b',
+                               array(
+                                       0 => '"19"',
+                                       1 => '"_blank"',
+                                       2 => '"css-class"',
+                                       3 => '"testtitle with whitespace"',
+                                       4 => '"&X=y&a=b"',
+                               ),
+                "index.php?id=19&X=y&a=b"
+                       ),
+                       'only page id and overwrite' => array(
+                               '42',
+                               '&a=b',
+                               array(
+                                       0 => '"42"',
+                                       1 => '-',
+                                       2 => '-',
+                                       3 => '-',
+                                       4 => '"&a=b"',
+                               ),
+                "index.php?id=42&a=b"
+                       ),
+                       'email' => array(
+                               'a@b.tld',
+                               '',
+                               array(
+                                       '"a@b.tld"',
+                               ),
+                'mailto:a@b.tld'
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider typoScriptConfigurationData
+        */
+       public function createTypolinkParameterArrayFromArgumentsReturnsExpectedArray($input, $additionalParametersFromFluid, $expected) {
+               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Uri\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
+               $subject = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Uri\\TypolinkViewHelper', array('dummy'));
+               $result = $subject->_call('createTypolinkParameterArrayFromArguments', $input, $additionalParametersFromFluid);
+               $this->assertSame($expected, $result);
+       }
+}