‪TYPO3CMS  ‪main
ModuleLinkViewHelper.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 Psr\Http\Message\ServerRequestInterface;
25 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28 
47 final class ‪ModuleLinkViewHelper extends AbstractViewHelper
48 {
49  use CompileWithRenderStatic;
50 
51  public function ‪initializeArguments(): void
52  {
53  $this->registerArgument('route', 'string', 'The route to link to', true);
54  $this->registerArgument('arguments', 'array', 'Additional link arguments', false, []);
55  $this->registerArgument('query', 'string', 'Additional link arguments as string');
56  $this->registerArgument('currentUrlParameterName', 'string', 'Add current url as given parameter');
57  }
58 
64  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
65  {
66  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
67  $parameters = $arguments['arguments'];
68  if ($arguments['query'] !== null) {
69  ArrayUtility::mergeRecursiveWithOverrule($parameters, GeneralUtility::explodeUrl2Array($arguments['query']));
70  }
72  $request = $renderingContext->getRequest();
73  if (!empty($arguments['currentUrlParameterName'])
74  && empty($arguments['arguments'][$arguments['currentUrlParameterName']])
75  && $request instanceof ServerRequestInterface
76  ) {
77  // If currentUrlParameterName is given and if that argument is not hand over yet, and if there is a request, fetch it from request
78  // @todo: We may want to deprecate fetching stuff from request and advise handing over a proper value as 'arguments' argument.
79  $parameters[$arguments['currentUrlParameterName']] = $request->getAttribute('normalizedParams')->getRequestUri();
80  }
81  return (string)$uriBuilder->buildUriFromRoute($arguments['route'], $parameters);
82  }
83 }
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\ViewHelpers
Definition: AvatarViewHelper.php:18
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35