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