-
Notifications
You must be signed in to change notification settings - Fork 639
/
TypolinkViewHelper.php
187 lines (170 loc) · 7.07 KB
/
TypolinkViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* A ViewHelper to create links from fields supported by the link wizard
*
* == Example ==
*
* {link} contains "19 _blank - "testtitle with whitespace" &X=y"
*
* <code title="minimal usage">
* <f:link.typolink parameter="{link}">
* Linktext
* </f:link.typolink>
* <output>
* <a href="index.php?id=19&X=y" title="testtitle with whitespace" target="_blank">
* Linktext
* </a>
* </output>
* </code>
*
* <code title="Full parameter usage">
* <f:link.typolink parameter="{link}" target="_blank" class="ico-class" title="some title" additionalParams="&u=b" additionalAttributes="{type:'button'}" useCacheHash="true">
* Linktext
* </f:link.typolink>
* </code>
* <output>
* <a href="index.php?id=19&X=y&u=b" title="some title" target="_blank" class="ico-class" type="button">
* Linktext
* </a>
* </output>
*/
class TypolinkViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Initialize ViewHelper arguments
*
* @throws Exception
*/
public function initializeArguments()
{
$this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
$this->registerArgument('target', 'string', '', false, '');
$this->registerArgument('class', 'string', '', false, '');
$this->registerArgument('title', 'string', '', false, '');
$this->registerArgument('additionalParams', 'string', '', false, '');
$this->registerArgument('additionalAttributes', 'array', '', false, []);
$this->registerArgument('useCacheHash', 'bool', '', false, false);
$this->registerArgument('addQueryString', 'bool', '', false, false);
$this->registerArgument('addQueryStringMethod', 'string', '', false, 'GET');
$this->registerArgument('addQueryStringExclude', 'string', '', false, '');
$this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
}
/**
* Render
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed|string
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$parameter = $arguments['parameter'] ?? '';
$target = $arguments['target'] ?? '';
$class = $arguments['class'] ?? '';
$title = $arguments['title'] ?? '';
$additionalParams = $arguments['additionalParams'] ?? '';
$additionalAttributes = $arguments['additionalAttributes'] ?? [];
$useCacheHash = $arguments['useCacheHash'] ?? false;
$addQueryString = $arguments['addQueryString'] ?? false;
$addQueryStringMethod = $arguments['addQueryStringMethod'] ?? 'GET';
$addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
$absolute = $arguments['absolute'] ?? false;
// Merge the $parameter with other arguments
$typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
// array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
$extraAttributes = [];
foreach ($additionalAttributes as $attributeName => $attributeValue) {
$extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
}
$aTagParams = implode(' ', $extraAttributes);
// If no link has to be rendered, the inner content will be returned as such
$content = (string)$renderChildrenClosure();
if ($parameter) {
/** @var ContentObjectRenderer $contentObject */
$contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$contentObject->start([], '');
$content = $contentObject->stdWrap(
$content,
[
'typolink.' => [
'parameter' => $typolinkParameter,
'ATagParams' => $aTagParams,
'useCacheHash' => $useCacheHash,
'addQueryString' => $addQueryString,
'addQueryString.' => [
'method' => $addQueryStringMethod,
'exclude' => $addQueryStringExclude
],
'forceAbsoluteUrl' => $absolute
]
]
);
}
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 $target
* @param string $class
* @param string $title
* @param string $additionalParams
*
* @return string The final TypoLink string
*/
protected static function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '')
{
$typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
$typolinkConfiguration = $typoLinkCodec->decode($parameter);
if (empty($typolinkConfiguration)) {
return $typolinkConfiguration;
}
// Override target if given in target argument
if ($target) {
$typolinkConfiguration['target'] = $target;
}
// Combine classes if given in both "parameter" string and "class" argument
if ($class) {
$classes = explode(' ', trim($typolinkConfiguration['class']) . ' ' . trim($class));
$typolinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
}
// Override title if given in title argument
if ($title) {
$typolinkConfiguration['title'] = $title;
}
// Combine additionalParams
if ($additionalParams) {
$typolinkConfiguration['additionalParams'] .= $additionalParams;
}
return $typoLinkCodec->encode($typolinkConfiguration);
}
}