‪TYPO3CMS  9.5
TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
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 
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 
75 class ‪TypolinkViewHelper extends AbstractViewHelper
76 {
77  use CompileWithRenderStatic;
78 
82  protected ‪$escapeOutput = false;
83 
89  public function ‪initializeArguments()
90  {
91  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
92  $this->registerArgument('target', 'string', '', false, '');
93  $this->registerArgument('class', 'string', '', false, '');
94  $this->registerArgument('title', 'string', '', false, '');
95  $this->registerArgument('additionalParams', 'string', '', false, '');
96  $this->registerArgument('additionalAttributes', 'array', '', false, []);
97  $this->registerArgument('useCacheHash', 'bool', '', false, false);
98  $this->registerArgument('addQueryString', 'bool', '', false, false);
99  $this->registerArgument('addQueryStringMethod', 'string', '', false, 'GET');
100  $this->registerArgument('addQueryStringExclude', 'string', '', false, '');
101  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
102  }
103 
114  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
115  {
116  $parameter = $arguments['parameter'] ?? '';
117  $target = $arguments['target'] ?? '';
118  $class = $arguments['class'] ?? '';
119  $title = $arguments['title'] ?? '';
120  $additionalParams = $arguments['additionalParams'] ?? '';
121  $additionalAttributes = $arguments['additionalAttributes'] ?? [];
122  $useCacheHash = $arguments['useCacheHash'] ?? false;
123  $addQueryString = $arguments['addQueryString'] ?? false;
124  $addQueryStringMethod = $arguments['addQueryStringMethod'] ?? 'GET';
125  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
126  $absolute = $arguments['absolute'] ?? false;
127 
128  // Merge the $parameter with other arguments
129  $typolinkParameter = ‪self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
130 
131  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
132  $extraAttributes = [];
133  foreach ($additionalAttributes as $attributeName => $attributeValue) {
134  $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
135  }
136  $aTagParams = implode(' ', $extraAttributes);
137 
138  // If no link has to be rendered, the inner content will be returned as such
139  $content = (string)$renderChildrenClosure();
140 
141  if ($parameter) {
143  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
144  $contentObject->start([], '');
145  $content = $contentObject->stdWrap(
146  $content,
147  [
148  'typolink.' => [
149  'parameter' => $typolinkParameter,
150  'ATagParams' => $aTagParams,
151  'useCacheHash' => $useCacheHash,
152  'addQueryString' => $addQueryString,
153  'addQueryString.' => [
154  'method' => $addQueryStringMethod,
155  'exclude' => $addQueryStringExclude
156  ],
157  'forceAbsoluteUrl' => $absolute
158  ]
159  ]
160  );
161  }
162 
163  return $content;
164  }
165 
177  protected static function ‪createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '')
178  {
179  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
180  $typolinkConfiguration = $typoLinkCodec->decode($parameter);
181  if (empty($typolinkConfiguration)) {
182  return $typolinkConfiguration;
183  }
184 
185  // Override target if given in target argument
186  if ($target) {
187  $typolinkConfiguration['target'] = $target;
188  }
189 
190  // Combine classes if given in both "parameter" string and "class" argument
191  if ($class) {
192  $classes = explode(' ', trim($typolinkConfiguration['class']) . ' ' . trim($class));
193  $typolinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
194  }
195 
196  // Override title if given in title argument
197  if ($title) {
198  $typolinkConfiguration['title'] = $title;
199  }
200 
201  // Combine additionalParams
202  if ($additionalParams) {
203  $typolinkConfiguration['additionalParams'] .= $additionalParams;
204  }
205 
206  return $typoLinkCodec->encode($typolinkConfiguration);
207  }
208 }
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45