‪TYPO3CMS  10.4
TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25 
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', 'Define where to display the linked URL', false, '');
93  $this->registerArgument('class', 'string', 'Define classes for the link element', false, '');
94  $this->registerArgument('title', 'string', 'Define the title for the link element', false, '');
95  $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, null);
96  $this->registerArgument('additionalParams', 'string', 'Additional query parameters to be attached to the resulting URL', false, '');
97  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes to be added directly to the resulting HTML tag', false, []);
98  // @deprecated useCacheHash
99  $this->registerArgument('useCacheHash', 'bool', 'Deprecated: You should not need this.', false, null);
100  $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URL', false, false);
101  $this->registerArgument('addQueryStringMethod', 'string', 'Method to use when keeping query parameters (only active if addQueryString is set)', false, 'GET');
102  $this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
103  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
104  $this->registerArgument('parts-as', 'string', 'Variable name containing typoLink parts (if any)', false, 'typoLinkParts');
105  }
106 
117  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
118  {
119  if (isset($arguments['useCacheHash'])) {
120  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);
121  }
122  $parameter = $arguments['parameter'] ?? '';
123  $partsAs = $arguments['parts-as'] ?? 'typoLinkParts';
124 
125  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
126  $typoLinkConfiguration = $typoLinkCodec->decode($parameter);
127  // Merge the $parameter with other arguments
128  $mergedTypoLinkConfiguration = static::mergeTypoLinkConfiguration($typoLinkConfiguration, $arguments);
129  $typoLinkParameter = $typoLinkCodec->encode($mergedTypoLinkConfiguration);
130 
131  // expose internal typoLink configuration to Fluid child context
132  $variableProvider = $renderingContext->getVariableProvider();
133  $variableProvider->add($partsAs, $typoLinkConfiguration);
134  // If no link has to be rendered, the inner content will be returned as such
135  $content = (string)$renderChildrenClosure();
136  // clean up exposed variables
137  $variableProvider->remove($partsAs);
138 
139  if ($parameter) {
140  $content = static::invokeContentObjectRenderer($arguments, $typoLinkParameter, $content);
141  }
142  return $content;
143  }
144 
145  protected static function ‪invokeContentObjectRenderer(array $arguments, string $typoLinkParameter, string $content): string
146  {
147  $addQueryString = $arguments['addQueryString'] ?? false;
148  $addQueryStringMethod = $arguments['addQueryStringMethod'] ?? 'GET';
149  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
150  $absolute = $arguments['absolute'] ?? false;
151  $aTagParams = static::serializeTagParameters($arguments);
152 
153  $instructions = [
154  'parameter' => $typoLinkParameter,
155  'ATagParams' => $aTagParams,
156  'forceAbsoluteUrl' => $absolute,
157  ];
158  if (isset($arguments['language']) && $arguments['language'] !== null) {
159  $instructions['language'] = $arguments['language'];
160  }
161  if ($addQueryString) {
162  $instructions['addQueryString'] = $addQueryString;
163  $instructions['addQueryString.'] = [
164  'method' => $addQueryStringMethod,
165  'exclude' => $addQueryStringExclude,
166  ];
167  }
168 
169  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
170  return $contentObject->stdWrap($content, ['typolink.' => $instructions]);
171  }
172 
173  protected static function ‪serializeTagParameters(array $arguments): string
174  {
175  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
176  $extraAttributes = [];
177  $additionalAttributes = $arguments['additionalAttributes'] ?? [];
178  foreach ($additionalAttributes as $attributeName => $attributeValue) {
179  $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
180  }
181  return implode(' ', $extraAttributes);
182  }
183 
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:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46