‪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\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  // @deprecated useCacheHash
72  $this->registerArgument('useCacheHash', 'bool', 'Deprecated: You should not need this.', false);
73  $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URL', false, false);
74  $this->registerArgument('addQueryStringMethod', 'string', 'Method to use when keeping query parameters (only active if addQueryString is set)', false, 'GET');
75  $this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
76  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
77  }
78 
85  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
86  {
87  if (isset($arguments['useCacheHash'])) {
88  trigger_error('Using the argument "noCacheHash" in <f:uri.typolink> ViewHelper has no effect anymore. Remove the argument in your fluid template, as it will result in a fatal error.', E_USER_DEPRECATED);
89  }
90  $parameter = $arguments['parameter'];
91 
92  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
93  $typoLinkConfiguration = $typoLinkCodec->decode($parameter);
94  $mergedTypoLinkConfiguration = static::mergeTypoLinkConfiguration($typoLinkConfiguration, $arguments);
95  $typoLinkParameter = $typoLinkCodec->encode($mergedTypoLinkConfiguration);
96 
97  $content = '';
98  if ($parameter) {
99  $content = static::invokeContentObjectRenderer($arguments, $typoLinkParameter);
100  }
101  return $content;
102  }
103 
104  protected static function ‪invokeContentObjectRenderer(array $arguments, string $typoLinkParameter): string
105  {
106  $addQueryString = $arguments['addQueryString'] ?? false;
107  $addQueryStringMethod = $arguments['addQueryStringMethod'] ?? 'GET';
108  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
109  $absolute = $arguments['absolute'] ?? false;
110 
111  $instructions = [
112  'parameter' => $typoLinkParameter,
113  'forceAbsoluteUrl' => $absolute,
114  ];
115  if (isset($arguments['language']) && $arguments['language'] !== null) {
116  $instructions['language'] = $arguments['language'];
117  }
118  if ($addQueryString) {
119  $instructions['addQueryString'] = $addQueryString;
120  $instructions['addQueryString.'] = [
121  'method' => $addQueryStringMethod,
122  'exclude' => $addQueryStringExclude,
123  ];
124  }
125 
126  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
127  return $contentObject->typoLink_URL($instructions);
128  }
129 
137  protected static function ‪mergeTypoLinkConfiguration(array $typoLinkConfiguration, array $arguments): array
138  {
139  if ($typoLinkConfiguration === []) {
140  return $typoLinkConfiguration;
141  }
142 
143  $additionalParameters = $arguments['additionalParams'] ?? '';
144 
145  // Combine additionalParams
146  if ($additionalParameters) {
147  $typoLinkConfiguration['additionalParams'] .= $additionalParameters;
148  }
149 
150  return $typoLinkConfiguration;
151  }
152 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:16
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46