‪TYPO3CMS  11.5
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 
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 
59 class ‪TypolinkViewHelper extends AbstractViewHelper
60 {
61  use CompileWithRenderStatic;
62 
66  public function ‪initializeArguments()
67  {
68  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
69  $this->registerArgument('additionalParams', 'string', 'stdWrap.typolink additionalParams', false, '');
70  $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);
71  $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URL', false, false);
72  $this->registerArgument('addQueryStringMethod', 'string', 'This argument is not evaluated anymore and will be removed in TYPO3 v12.');
73  $this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
74  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
75  }
76 
83  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
84  {
85  $parameter = $arguments['parameter'] ?? '';
86 
87  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
88  $typoLinkConfiguration = $typoLinkCodec->decode($parameter);
89  $mergedTypoLinkConfiguration = static::mergeTypoLinkConfiguration($typoLinkConfiguration, $arguments);
90  $typoLinkParameter = $typoLinkCodec->encode($mergedTypoLinkConfiguration);
91 
92  $content = '';
93  if ($parameter) {
94  $content = static::invokeContentObjectRenderer($arguments, $typoLinkParameter);
95  }
96  return $content;
97  }
98 
99  protected static function ‪invokeContentObjectRenderer(array $arguments, string $typoLinkParameter): string
100  {
101  if (isset($arguments['addQueryStringMethod'])) {
102  trigger_error('Using the argument "addQueryStringMethod" in <f:uri.typolink> ViewHelper has no effect anymore and will be removed in TYPO3 v12. Remove the argument in your fluid template, as it will result in a fatal error.', E_USER_DEPRECATED);
103  }
104  $addQueryString = $arguments['addQueryString'] ?? false;
105  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
106  $absolute = $arguments['absolute'] ?? false;
107 
108  $instructions = [
109  'parameter' => $typoLinkParameter,
110  'forceAbsoluteUrl' => $absolute,
111  ];
112  if (isset($arguments['language']) && $arguments['language'] !== null) {
113  $instructions['language'] = $arguments['language'];
114  }
115  if ($addQueryString) {
116  $instructions['addQueryString'] = $addQueryString;
117  $instructions['addQueryString.'] = [
118  'exclude' => $addQueryStringExclude,
119  ];
120  }
121 
122  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
123  return $contentObject->typoLink_URL($instructions);
124  }
125 
133  protected static function ‪mergeTypoLinkConfiguration(array $typoLinkConfiguration, array $arguments): array
134  {
135  if ($typoLinkConfiguration === []) {
136  return $typoLinkConfiguration;
137  }
138 
139  $additionalParameters = $arguments['additionalParams'] ?? '';
140 
141  // Combine additionalParams
142  if ($additionalParameters) {
143  $typoLinkConfiguration['additionalParams'] .= $additionalParameters;
144  }
145 
146  return $typoLinkConfiguration;
147  }
148 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:16
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50