[FEATURE] Typolink ViewHelper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Link / TypolinkViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
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 links from fields supported by the link wizard
23 *
24 * == Example ==
25 *
26 * {link} contains "19 _blank - "testtitle with whitespace" &X=y"
27 *
28 * <code title="minimal usage">
29 * <f:link.typolink parameter="{link}">
30 * Linktext
31 * </f:link.typolink>
32 * <output>
33 * <a href="index.php?id=19&X=y" title="testtitle with whitespace" target="_blank">
34 * Linktext
35 * </a>
36 * </output>
37 * </code>
38 *
39 * <code title="Full parameter usage">
40 * <f:link.typolink parameter="{link}" target="_blank" class="ico-class" title="some title" additionalParams="&u=b" additionalAttributes="{type:'button'}">
41 * Linktext
42 * </f:link.typolink>
43 * </code>
44 * <output>
45 * <a href="index.php?id=19&X=y&u=b" title="some title" target="_blank" class="ico-class" type="button">
46 * Linktext
47 * </a>
48 * </output>
49 *
50 */
51 class TypolinkViewHelper extends AbstractViewHelper {
52
53 /**
54 * Render
55 *
56 * @param string $parameter stdWrap.typolink style parameter string
57 * @param string $target
58 * @param string $class
59 * @param string $title
60 * @param string $additionalParams
61 * @param array $additionalAttributes
62 *
63 * @return string
64 */
65 public function render($parameter, $target = '', $class = '', $title = '', $additionalParams = '', $additionalAttributes = array()) {
66 // Merge the $parameter with other arguments
67 $typolinkParameter = $this->createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
68
69 // array(param1 -> value1, param2 -> value2) --> "param1=value1 param2=>value2" for typolink.ATagParams
70 $extraAttributes = array();
71 foreach ($additionalAttributes as $attributeName => $attributeValue) {
72 $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
73 }
74 $aTagParams = implode(' ', $extraAttributes);
75
76 // If no link has to be rendered, the inner content will be returned as such
77 $content = $this->renderChildren();
78
79 if ($parameter) {
80 /** @var ContentObjectRenderer $contentObject */
81 $contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
82 $contentObject->start(array(), '');
83 $content = $contentObject->stdWrap(
84 $content,
85 array(
86 'typolink.' => array(
87 'parameter' => implode(' ', $typolinkParameter),
88 'ATagParams' => $aTagParams,
89 )
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 $target
102 * @param string $class
103 * @param string $title
104 * @param string $additionalParams
105 *
106 * @return array Final merged typolink.parameter as array to be imploded with empty string later
107 */
108 protected function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '') {
109 // Explode $parameter by whitespace and remove any " around resulting array values
110 $parameterArray = GeneralUtility::unQuoteFilenames($parameter, TRUE);
111
112 if (empty($parameterArray)) {
113 return array();
114 }
115
116 // Extend to 4 elements
117 $typolinkConfiguration = array_pad($parameterArray, 4, '-');
118
119 // Override target if given in target argument
120 if ($target) {
121 $typolinkConfiguration[1] = $target;
122 }
123
124 // Combine classes if given in both "parameter" string and "class" argument
125 if ($class) {
126 $typolinkConfiguration[2] = $typolinkConfiguration[2] !== '-' ? $typolinkConfiguration[2] . ' ' : '';
127 $typolinkConfiguration[2] .= $class;
128 }
129
130 // Override title if given in title argument
131 if ($title) {
132 $typolinkConfiguration[3] = $title;
133 }
134
135 // Combine additionalParams
136 if ($additionalParams) {
137 $typolinkConfiguration[4] .= $additionalParams;
138 }
139
140 // Unset unused parameters again from the end, wrap all given values with "
141 $reverseSortedParameters = array_reverse($typolinkConfiguration, TRUE);
142 $aValueWasSet = FALSE;
143 foreach ($reverseSortedParameters as $position => $value) {
144 if ($value === '-' && !$aValueWasSet) {
145 unset($typolinkConfiguration[$position]);
146 } else {
147 $aValueWasSet = TRUE;
148 if ($value !== '-') {
149 $typolinkConfiguration[$position] = '"' . $value . '"';
150 }
151 }
152 }
153
154 return $typolinkConfiguration;
155 }
156 }