‪TYPO3CMS  ‪main
TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
25 use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28 
93 final class ‪TypolinkViewHelper extends AbstractViewHelper
94 {
95  use CompileWithRenderStatic;
96 
100  protected ‪$escapeOutput = false;
101 
102  public function ‪initializeArguments(): void
103  {
104  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
105  $this->registerArgument('target', 'string', 'Define where to display the linked URL', false, '');
106  $this->registerArgument('class', 'string', 'Define classes for the link element', false, '');
107  $this->registerArgument('title', 'string', 'Define the title for the link element', false, '');
108  $this->registerArgument('language', 'string', 'link to a specific language - defaults to the current language, use a language ID or "current" to enforce a specific language', false);
109  $this->registerArgument('additionalParams', 'string', 'Additional query parameters to be attached to the resulting URL', false, '');
110  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes to be added directly to the resulting HTML tag', false, []);
111  $this->registerArgument('addQueryString', 'string', 'If set, the current query parameters will be kept in the URL. If set to "untrusted", then ALL query parameters will be added. Be aware, that this might lead to problems when the generated link is cached.', false, false);
112  $this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
113  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
114  $this->registerArgument('parts-as', 'string', 'Variable name containing typoLink parts (if any)', false, 'typoLinkParts');
115  $this->registerArgument('textWrap', 'string', 'Wrap the link using the typoscript "wrap" data type', false, '');
116  }
117 
122  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
123  {
124  $parameter = $arguments['parameter'] ?? '';
125  $partsAs = $arguments['parts-as'] ?? 'typoLinkParts';
126 
127  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
128  $typoLinkConfiguration = $typoLinkCodec->decode((string)$parameter);
129  // Merge the $parameter with other arguments
130  $mergedTypoLinkConfiguration = ‪self::mergeTypoLinkConfiguration($typoLinkConfiguration, $arguments);
131  $typoLinkParameter = $typoLinkCodec->encode($mergedTypoLinkConfiguration);
132 
133  // expose internal typoLink configuration to Fluid child context
134  $variableProvider = new ScopedVariableProvider($renderingContext->getVariableProvider(), new StandardVariableProvider([$partsAs => $typoLinkConfiguration]));
135  $renderingContext->setVariableProvider($variableProvider);
136  // If no link has to be rendered, the inner content will be returned as such
137  $content = (string)$renderChildrenClosure();
138  // clean up exposed variables
139  $renderingContext->setVariableProvider($variableProvider->getGlobalVariableProvider());
140 
141  if ($parameter) {
142  $content = ‪self::invokeContentObjectRenderer($arguments, $typoLinkParameter, $content);
143  }
144  return $content;
145  }
146 
147  protected static function ‪invokeContentObjectRenderer(array $arguments, string $typoLinkParameter, string $content): string
148  {
149  $addQueryString = $arguments['addQueryString'] ?? false;
150  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
151  $absolute = $arguments['absolute'] ?? false;
152  $aTagParams = ‪self::serializeTagParameters($arguments);
153 
154  $instructions = [
155  'parameter' => $typoLinkParameter,
156  'ATagParams' => $aTagParams,
157  'forceAbsoluteUrl' => $absolute,
158  ];
159  if (isset($arguments['language']) && $arguments['language'] !== null) {
160  $instructions['language'] = (string)$arguments['language'];
161  }
162  if ($addQueryString && $addQueryString !== 'false') {
163  $instructions['addQueryString'] = $addQueryString;
164  $instructions['addQueryString.'] = [
165  'exclude' => $addQueryStringExclude,
166  ];
167  }
168  if ((string)($arguments['textWrap'] ?? '') !== '') {
169  $instructions['ATagBeforeWrap'] = true;
170  $instructions['wrap'] = $arguments['textWrap'];
171  }
172 
173  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
174  return $contentObject->typoLink($content, $instructions);
175  }
176 
177  protected static function ‪serializeTagParameters(array $arguments): string
178  {
179  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
180  $extraAttributes = [];
181  $additionalAttributes = $arguments['additionalAttributes'] ?? [];
182  foreach ($additionalAttributes as $attributeName => $attributeValue) {
183  $extraAttributes[] = $attributeName . '="' . htmlspecialchars((string)$attributeValue) . '"';
184  }
185  return implode(' ', $extraAttributes);
186  }
187 
191  protected static function ‪mergeTypoLinkConfiguration(array $typoLinkConfiguration, array $arguments): array
192  {
193  if ($typoLinkConfiguration === []) {
194  return $typoLinkConfiguration;
195  }
196 
197  $target = $arguments['target'] ?? '';
198  $class = $arguments['class'] ?? '';
199  $title = $arguments['title'] ?? '';
200  $additionalParams = $arguments['additionalParams'] ?? '';
201 
202  // Override target if given in target argument
203  if ($target) {
204  $typoLinkConfiguration['target'] = $target;
205  }
206  // Combine classes if given in both "parameter" string and "class" argument
207  if ($class) {
208  $classes = explode(' ', trim($typoLinkConfiguration['class']) . ' ' . trim($class));
209  $typoLinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
210  }
211  // Override title if given in title argument
212  if ($title) {
213  $typoLinkConfiguration['title'] = $title;
214  }
215  // Combine additionalParams
216  if ($additionalParams) {
217  $typoLinkConfiguration['additionalParams'] .= $additionalParams;
218  }
219 
220  return $typoLinkConfiguration;
221  }
222 }
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52