‪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\ViewHelper\AbstractViewHelper;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
26 
61 final class ‪TypolinkViewHelper extends AbstractViewHelper
62 {
63  use CompileWithRenderStatic;
64 
65  public function ‪initializeArguments(): void
66  {
67  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
68  $this->registerArgument('additionalParams', 'string', 'stdWrap.typolink additionalParams', false, '');
69  $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);
70  $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);
71  $this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
72  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
73  }
74 
75  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
76  {
77  $parameter = $arguments['parameter'] ?? '';
78 
79  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
80  $typoLinkConfiguration = $typoLinkCodec->decode((string)$parameter);
81  $mergedTypoLinkConfiguration = ‪self::mergeTypoLinkConfiguration($typoLinkConfiguration, $arguments);
82  $typoLinkParameter = $typoLinkCodec->encode($mergedTypoLinkConfiguration);
83 
84  $content = '';
85  if ($parameter) {
86  $content = ‪self::invokeContentObjectRenderer($arguments, $typoLinkParameter);
87  }
88  return $content;
89  }
90 
91  protected static function ‪invokeContentObjectRenderer(array $arguments, string $typoLinkParameter): string
92  {
93  $addQueryString = $arguments['addQueryString'] ?? false;
94  $addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
95  $absolute = $arguments['absolute'] ?? false;
96 
97  $instructions = [
98  'parameter' => $typoLinkParameter,
99  'forceAbsoluteUrl' => $absolute,
100  ];
101  if (isset($arguments['language']) && $arguments['language'] !== null) {
102  $instructions['language'] = (string)$arguments['language'];
103  }
104  if ($addQueryString && $addQueryString !== 'false') {
105  $instructions['addQueryString'] = $addQueryString;
106  $instructions['addQueryString.'] = [
107  'exclude' => $addQueryStringExclude,
108  ];
109  }
110 
111  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
112  return $contentObject->createUrl($instructions);
113  }
114 
118  protected static function ‪mergeTypoLinkConfiguration(array $typoLinkConfiguration, array $arguments): array
119  {
120  if ($typoLinkConfiguration === []) {
121  return $typoLinkConfiguration;
122  }
123 
124  $additionalParameters = $arguments['additionalParams'] ?? '';
125 
126  // Combine additionalParams
127  if ($additionalParameters) {
128  $typoLinkConfiguration['additionalParams'] .= $additionalParameters;
129  }
130 
131  return $typoLinkConfiguration;
132  }
133 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:18
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52