‪TYPO3CMS  ‪main
ActionViewHelper.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 
25 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
26 
43 final class ‪ActionViewHelper extends AbstractTagBasedViewHelper
44 {
48  protected ‪$tagName = 'a';
49 
50  public function ‪initializeArguments(): void
51  {
52  parent::initializeArguments();
53  $this->registerUniversalTagAttributes();
54  $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
55  $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
56  $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
57  $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
58  $this->registerArgument('action', 'string', 'Target action');
59  $this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used');
60  $this->registerArgument('extensionName', 'string', 'Target Extension Name (without `tx_` prefix and no underscores). If NULL the current extension name is used');
61  $this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
62  $this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
63  $this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter');
64  $this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.');
65  $this->registerArgument('section', 'string', 'The anchor to be added to the URI');
66  $this->registerArgument('format', 'string', 'The requested format, e.g. ".html');
67  $this->registerArgument('linkAccessRestrictedPages', 'bool', 'If set, links pointing to access restricted pages will still link to the page even though the page cannot be accessed.');
68  $this->registerArgument('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)');
69  $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute');
70  $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URI');
71  $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = TRUE');
72  $this->registerArgument('arguments', 'array', 'Arguments for the controller action, associative array');
73  }
74 
75  public function ‪render(): string
76  {
78  $renderingContext = $this->renderingContext;
79  $request = $renderingContext->getRequest();
80  if (!$request instanceof ‪RequestInterface) {
81  throw new \RuntimeException(
82  'ViewHelper f:link.action can be used only in extbase context and needs a request implementing extbase RequestInterface.',
83  1639818540
84  );
85  }
86 
87  $action = $this->arguments['action'];
88  $controller = $this->arguments['controller'];
89  $extensionName = $this->arguments['extensionName'];
90  $pluginName = $this->arguments['pluginName'];
91  $pageUid = (int)$this->arguments['pageUid'] ?: null;
92  $pageType = (int)$this->arguments['pageType'];
93  $noCache = (bool)$this->arguments['noCache'];
94  $section = (string)$this->arguments['section'];
95  $format = (string)$this->arguments['format'];
96  $linkAccessRestrictedPages = (bool)$this->arguments['linkAccessRestrictedPages'];
97  $additionalParams = (array)$this->arguments['additionalParams'];
98  $absolute = (bool)$this->arguments['absolute'];
99  $addQueryString = (bool)$this->arguments['addQueryString'];
100  $argumentsToBeExcludedFromQueryString = (array)$this->arguments['argumentsToBeExcludedFromQueryString'];
101  $parameters = $this->arguments['arguments'];
102 
103  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
104  $uriBuilder
105  ->reset()
106  ->setRequest($request)
107  ->setTargetPageType($pageType)
108  ->setNoCache($noCache)
109  ->setSection($section)
110  ->setFormat($format)
111  ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
112  ->setArguments($additionalParams)
113  ->setCreateAbsoluteUri($absolute)
114  ->setAddQueryString($addQueryString)
115  ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
116  ;
117 
119  $uriBuilder->setTargetPageUid((int)$pageUid);
120  }
121 
122  $uri = $uriBuilder->uriFor($action, $parameters, $controller, $extensionName, $pluginName);
123  if ($uri === '') {
124  return $this->renderChildren();
125  }
126  $this->tag->addAttribute('href', $uri);
127  $this->tag->setContent($this->renderChildren());
128  $this->tag->forceClosingTag(true);
129  return $this->tag->render();
130  }
131 }
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:42
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35