‪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 
20 use Psr\Http\Message\ServerRequestInterface;
25 use ‪TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
32 
49 final class ‪ActionViewHelper extends AbstractTagBasedViewHelper
50 {
54  protected ‪$tagName = 'a';
55 
56  public function ‪initializeArguments(): void
57  {
58  parent::initializeArguments();
59  $this->registerUniversalTagAttributes();
60  $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
61  $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
62  $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
63  $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
64  $this->registerArgument('action', 'string', 'Target action');
65  $this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used');
66  $this->registerArgument('extensionName', 'string', 'Target Extension Name (without `tx_` prefix and no underscores). If NULL the current extension name is used');
67  $this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
68  $this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
69  $this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter');
70  $this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.');
71  $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);
72  $this->registerArgument('section', 'string', 'The anchor to be added to the URI');
73  $this->registerArgument('format', 'string', 'The requested format, e.g. ".html');
74  $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.');
75  $this->registerArgument('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)');
76  $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute');
77  $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);
78  $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = TRUE');
79  $this->registerArgument('arguments', 'array', 'Arguments for the controller action, associative array');
80  }
81 
82  public function ‪render(): string
83  {
85  $renderingContext = $this->renderingContext;
86  $request = $renderingContext->getRequest();
87  if ($request instanceof ExtbaseRequestInterface) {
88  return $this->‪renderWithExtbaseContext($request);
89  }
90  if ($request instanceof ServerRequestInterface && ‪ApplicationType::fromRequest($request)->isFrontend()) {
91  return $this->‪renderFrontendLinkWithCoreContext($request);
92  }
93  throw new \RuntimeException(
94  'The rendering context of ViewHelper f:link.action is missing a valid request object.',
95  1690365240
96  );
97  }
98 
99  protected function ‪renderFrontendLinkWithCoreContext(ServerRequestInterface $request): string
100  {
101  // No support for following arguments:
102  // * format
103  $pageUid = (int)($this->arguments['pageUid'] ?? 0);
104  $pageType = (int)($this->arguments['pageType'] ?? 0);
105  $noCache = (bool)($this->arguments['noCache'] ?? false);
107  $language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null;
109  $section = $this->arguments['section'] ?? null;
110  $linkAccessRestrictedPages = (bool)($this->arguments['linkAccessRestrictedPages'] ?? false);
112  $additionalParams = $this->arguments['additionalParams'] ?? null;
113  $absolute = (bool)($this->arguments['absolute'] ?? false);
115  $addQueryString = $this->arguments['addQueryString'] ?? false;
117  $argumentsToBeExcludedFromQueryString = $this->arguments['argumentsToBeExcludedFromQueryString'] ?? null;
119  $action = $this->arguments['action'] ?? null;
121  $controller = $this->arguments['controller'] ?? null;
123  $extensionName = $this->arguments['extensionName'] ?? null;
125  $pluginName = $this->arguments['pluginName'] ?? null;
127  $arguments = $this->arguments['arguments'] ?? [];
128 
129  $allExtbaseArgumentsAreSet = (
130  is_string($extensionName) && $extensionName !== ''
131  && is_string($pluginName) && $pluginName !== ''
132  && is_string($controller) && $controller !== ''
133  && is_string($action) && $action !== ''
134  );
135  if (!$allExtbaseArgumentsAreSet) {
136  throw new \RuntimeException(
137  'ViewHelper f:link.action needs either all extbase arguments set'
138  . ' ("extensionName", "pluginName", "controller", "action")'
139  . ' or needs a request implementing extbase RequestInterface.',
140  1690370264
141  );
142  }
143 
144  // Provide extbase default and custom arguments as prefixed additional params
145  $extbaseArgumentNamespace = 'tx_'
146  . str_replace('_', '', strtolower($extensionName))
147  . '_'
148  . str_replace('_', '', strtolower($pluginName));
149  $additionalParams ??= [];
150  $additionalParams[$extbaseArgumentNamespace] = array_replace(
151  [
152  'controller' => $controller,
153  'action' => $action,
154  ],
155  $arguments
156  );
157 
158  $typolinkConfiguration = [
159  'parameter' => $pageUid,
160  ];
161  if ($pageType) {
162  $typolinkConfiguration['parameter'] .= ',' . $pageType;
163  }
164  if ($language !== null) {
165  $typolinkConfiguration['language'] = $language;
166  }
167  if ($noCache) {
168  $typolinkConfiguration['no_cache'] = 1;
169  }
170  if ($section) {
171  $typolinkConfiguration['section'] = $section;
172  }
173  if ($linkAccessRestrictedPages) {
174  $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
175  }
176  $typolinkConfiguration['additionalParams'] = ‪HttpUtility::buildQueryString($additionalParams, '&');
177  if ($absolute) {
178  $typolinkConfiguration['forceAbsoluteUrl'] = true;
179  }
180  if ($addQueryString && $addQueryString !== 'false') {
181  $typolinkConfiguration['addQueryString'] = $addQueryString;
182  if ($argumentsToBeExcludedFromQueryString !== []) {
183  $typolinkConfiguration['addQueryString.']['exclude'] = implode(',', $argumentsToBeExcludedFromQueryString);
184  }
185  }
186 
187  try {
188  $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
189  $cObj->setRequest($request);
190  $linkFactory = GeneralUtility::makeInstance(LinkFactory::class);
191  $linkResult = $linkFactory->create((string)$this->renderChildren(), $typolinkConfiguration, $cObj);
192 
193  // Removing TypoLink target here to ensure same behaviour with extbase uri builder in this context.
194  $linkResultAttributes = $linkResult->getAttributes();
195  unset($linkResultAttributes['target']);
196 
197  $this->tag->addAttributes($linkResultAttributes);
198  $this->tag->setContent($this->renderChildren());
199  $this->tag->forceClosingTag(true);
200  return $this->tag->render();
201  } catch (‪UnableToLinkException) {
202  return (string)$this->renderChildren();
203  }
204  }
205 
206  protected function ‪renderWithExtbaseContext(ExtbaseRequestInterface $request): string
207  {
208  $action = $this->arguments['action'];
209  $controller = $this->arguments['controller'];
210  $extensionName = $this->arguments['extensionName'];
211  $pluginName = $this->arguments['pluginName'];
212  $pageUid = (int)$this->arguments['pageUid'] ?: null;
213  $pageType = (int)($this->arguments['pageType'] ?? 0);
214  $noCache = (bool)($this->arguments['noCache'] ?? false);
215  $language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null;
216  $section = (string)$this->arguments['section'];
217  $format = (string)$this->arguments['format'];
218  $linkAccessRestrictedPages = (bool)($this->arguments['linkAccessRestrictedPages'] ?? false);
219  $additionalParams = (array)$this->arguments['additionalParams'];
220  $absolute = (bool)($this->arguments['absolute'] ?? false);
221  $addQueryString = $this->arguments['addQueryString'] ?? false;
222  $argumentsToBeExcludedFromQueryString = (array)$this->arguments['argumentsToBeExcludedFromQueryString'];
223  $parameters = $this->arguments['arguments'];
224 
225  $uriBuilder = GeneralUtility::makeInstance(ExtbaseUriBuilder::class);
226  $uriBuilder
227  ->reset()
228  ->setRequest($request)
229  ->setTargetPageType($pageType)
230  ->setNoCache($noCache)
231  ->setLanguage($language)
232  ->setSection($section)
233  ->setFormat($format)
234  ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
235  ->setArguments($additionalParams)
236  ->setCreateAbsoluteUri($absolute)
237  ->setAddQueryString($addQueryString)
238  ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
239  ;
240 
242  $uriBuilder->setTargetPageUid((int)$pageUid);
243  }
244  $uri = $uriBuilder->uriFor($action, $parameters, $controller, $extensionName, $pluginName);
245  if ($uri === '') {
246  return $this->renderChildren();
247  }
248  $this->tag->addAttribute('href', $uri);
249  $this->tag->setContent($this->renderChildren());
250  $this->tag->forceClosingTag(true);
251  return $this->tag->render();
252  }
253 }
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:124
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:24
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55