[CLEANUP] Make use of possible imports and remove unused ones
[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\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20
21 /**
22 * A ViewHelper to create uris from fields supported by the link wizard
23 *
24 * == Example ==
25 *
26 * {link} contains "19 - - - &X=y"
27 * Please note that due to the nature of typolink you have to provide a
28 * full set of parameters if you use the parameter only. Target, class
29 * and title will be discarded.
30 *
31 * <code title="minimal usage">
32 * <f:uri.typolink parameter="{link}" />
33 * <output>
34 * index.php?id=19&X=y
35 * </output>
36 * </code>
37 *
38 * <code title="Full parameter usage">
39 * <f:uri.typolink parameter="{link}" additionalParams="&u=b" />
40 * </code>
41 * <output>
42 * index.php?id=19&X=y&u=b
43 * </output>
44 *
45 */
46 class TypolinkViewHelper extends AbstractViewHelper {
47
48 /**
49 * Render
50 *
51 * @param string $parameter stdWrap.typolink style parameter string
52 * @param string $additionalParams
53 *
54 * @return string
55 */
56 public function render($parameter, $additionalParams = '') {
57 // Merge the $parameter with other arguments
58 $typolinkParameter = $this->createTypolinkParameterArrayFromArguments($parameter, $additionalParams);
59
60 $content = '';
61
62 if ($parameter) {
63 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
64 $content = $contentObject->typoLink_URL(
65 array(
66 'parameter' => implode(' ', $typolinkParameter),
67 )
68 );
69 }
70
71 return $content;
72 }
73
74 /**
75 * Transforms ViewHelper arguments to typo3link.parameters.typoscript option as array.
76 *
77 * @param string $parameter Example: 19 _blank - "testtitle with whitespace" &X=y
78 * @param string $additionalParameters
79 *
80 * @return array Final merged typolink.parameter as array to be imploded with empty string later
81 */
82 protected function createTypolinkParameterArrayFromArguments($parameter, $additionalParameters = '') {
83 // Explode $parameter by whitespace and remove any " around resulting array values
84 $parameterArray = GeneralUtility::unQuoteFilenames($parameter, TRUE);
85
86 if (empty($parameterArray)) {
87 return array();
88 }
89
90 // Extend to 4 elements
91 $typolinkConfiguration = array_pad($parameterArray, 4, '-');
92
93 // Combine additionalParameters
94 if ($additionalParameters) {
95 $typolinkConfiguration[4] .= $additionalParameters;
96 }
97
98 // Unset unused parameters again from the end, wrap all given values with "
99 $reverseSortedParameters = array_reverse($typolinkConfiguration, TRUE);
100 $aValueWasSet = FALSE;
101 foreach ($reverseSortedParameters as $position => $value) {
102 if ($value === '-' && !$aValueWasSet) {
103 unset($typolinkConfiguration[$position]);
104 } else {
105 $aValueWasSet = TRUE;
106 if ($value !== '-') {
107 $typolinkConfiguration[$position] = '"' . $value . '"';
108 }
109 }
110 }
111 return $typolinkConfiguration;
112 }
113 }