[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / TypolinkViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
21 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
23 /**
24 * A ViewHelper to create uris from fields supported by the link wizard
25 *
26 * == Example ==
27 *
28 * {link} contains "19 - - - &X=y"
29 * Please note that due to the nature of typolink you have to provide a
30 * full set of parameters if you use the parameter only. Target, class
31 * and title will be discarded.
32 *
33 * <code title="minimal usage">
34 * <f:uri.typolink parameter="{link}" />
35 * <output>
36 * index.php?id=19&X=y
37 * </output>
38 * </code>
39 *
40 * <code title="Full parameter usage">
41 * <f:uri.typolink parameter="{link}" additionalParams="&u=b" />
42 * </code>
43 * <output>
44 * index.php?id=19&X=y&u=b
45 * </output>
46 *
47 */
48 class TypolinkViewHelper extends AbstractViewHelper implements CompilableInterface {
49
50 /**
51 * Render
52 *
53 * @param string $parameter stdWrap.typolink style parameter string
54 * @param string $additionalParams
55 *
56 * @return string
57 */
58 public function render($parameter, $additionalParams = '') {
59 return static::renderStatic(
60 array(
61 'parameter' => $parameter,
62 'additionalParams' => $additionalParams
63 ),
64 $this->buildRenderChildrenClosure(),
65 $this->renderingContext
66 );
67 }
68
69 /**
70 * @param array $arguments
71 * @param callable $renderChildrenClosure
72 * @param RenderingContextInterface $renderingContext
73 *
74 * @return string
75 */
76 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
77 $parameter = $arguments['parameter'];
78 $additionalParams = $arguments['additionalParams'];
79
80 // Merge the $parameter with other arguments
81 $typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $additionalParams);
82
83 $content = '';
84
85 if ($parameter) {
86 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
87 $content = $contentObject->typoLink_URL(
88 array(
89 'parameter' => implode(' ', $typolinkParameter),
90 )
91 );
92 }
93
94 return $content;
95 }
96
97 /**
98 * Transforms ViewHelper arguments to typo3link.parameters.typoscript option as array.
99 *
100 * @param string $parameter Example: 19 _blank - "testtitle with whitespace" &X=y
101 * @param string $additionalParameters
102 *
103 * @return array Final merged typolink.parameter as array to be imploded with empty string later
104 */
105 static protected function createTypolinkParameterArrayFromArguments($parameter, $additionalParameters = '') {
106 // Explode $parameter by whitespace and remove any " around resulting array values
107 $parameterArray = GeneralUtility::unQuoteFilenames($parameter, TRUE);
108
109 if (empty($parameterArray)) {
110 return array();
111 }
112
113 // Extend to 4 elements
114 $typolinkConfiguration = array_pad($parameterArray, 4, '-');
115
116 // Combine additionalParameters
117 if ($additionalParameters) {
118 $typolinkConfiguration[4] .= $additionalParameters;
119 }
120
121 // Unset unused parameters again from the end, wrap all given values with "
122 $reverseSortedParameters = array_reverse($typolinkConfiguration, TRUE);
123 $aValueWasSet = FALSE;
124 foreach ($reverseSortedParameters as $position => $value) {
125 if ($value === '-' && !$aValueWasSet) {
126 unset($typolinkConfiguration[$position]);
127 } else {
128 $aValueWasSet = TRUE;
129 if ($value !== '-') {
130 $typolinkConfiguration[$position] = '"' . $value . '"';
131 }
132 }
133 }
134 return $typolinkConfiguration;
135 }
136
137 }