‪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;
24 use ‪TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
30 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
32 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
33 
48 final class ‪ActionViewHelper extends AbstractViewHelper
49 {
50  use CompileWithRenderStatic;
51 
52  public function ‪initializeArguments(): void
53  {
54  $this->registerArgument('action', 'string', 'Target action');
55  $this->registerArgument('arguments', 'array', 'Arguments', false, []);
56  $this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used');
57  $this->registerArgument('extensionName', 'string', 'Target Extension Name (without `tx_` prefix and no underscores). If NULL the current extension name is used');
58  $this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
59  $this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
60  $this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter', false, 0);
61  $this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.', false);
62  $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);
63  $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
64  $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
65  $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.', false, false);
66  $this->registerArgument('additionalParams', 'array', 'additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)', false, []);
67  $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
68  $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);
69  $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
70  }
71 
72  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
73  {
75  $request = $renderingContext->getRequest();
76  if ($request instanceof ExtbaseRequestInterface) {
77  return ‪self::renderWithExtbaseContext($request, $arguments);
78  }
79 
80  if ($request instanceof ServerRequestInterface && ‪ApplicationType::fromRequest($request)->isFrontend()) {
81  return ‪self::renderFrontendLinkWithCoreContext($request, $arguments, $renderChildrenClosure);
82  }
83  throw new \RuntimeException(
84  'The rendering context of ViewHelper f:uri.action is missing a valid request object.',
85  1690360598
86  );
87  }
88 
89  protected static function ‪renderFrontendLinkWithCoreContext(ServerRequestInterface $request, array $arguments, \Closure $renderChildrenClosure): string
90  {
91  // No support for following arguments:
92  // * format
93  $pageUid = (int)($arguments['pageUid'] ?? 0);
94  $pageType = (int)($arguments['pageType'] ?? 0);
95  $noCache = (bool)($arguments['noCache'] ?? false);
97  $language = isset($arguments['language']) ? (string)$arguments['language'] : null;
99  $section = $arguments['section'] ?? null;
100  $linkAccessRestrictedPages = (bool)($arguments['linkAccessRestrictedPages'] ?? false);
102  $additionalParams = $arguments['additionalParams'] ?? null;
103  $absolute = (bool)($arguments['absolute'] ?? false);
105  $addQueryString = $arguments['addQueryString'] ?? false;
107  $argumentsToBeExcludedFromQueryString = $arguments['argumentsToBeExcludedFromQueryString'] ?? null;
109  $action = $arguments['action'] ?? null;
111  $controller = $arguments['controller'] ?? null;
113  $extensionName = $arguments['extensionName'] ?? null;
115  $pluginName = $arguments['pluginName'] ?? null;
117  $arguments = $arguments['arguments'] ?? [];
118 
119  $allExtbaseArgumentsAreSet = (
120  is_string($extensionName) && $extensionName !== ''
121  && is_string($pluginName) && $pluginName !== ''
122  && is_string($controller) && $controller !== ''
123  && is_string($action) && $action !== ''
124  );
125  if (!$allExtbaseArgumentsAreSet) {
126  throw new \RuntimeException(
127  'ViewHelper f:uri.action needs either all extbase arguments set'
128  . ' ("extensionName", "pluginName", "controller", "action")'
129  . ' or needs a request implementing extbase RequestInterface.',
130  1639819692
131  );
132  }
133 
134  // Provide extbase default and custom arguments as prefixed additional params
135  $extbaseArgumentNamespace = 'tx_'
136  . str_replace('_', '', strtolower($extensionName))
137  . '_'
138  . str_replace('_', '', strtolower($pluginName));
139  $additionalParams ??= [];
140  $additionalParams[$extbaseArgumentNamespace] = array_replace(
141  [
142  'controller' => $controller,
143  'action' => $action,
144  ],
145  $arguments
146  );
147 
148  $typolinkConfiguration = [
149  'parameter' => $pageUid,
150  ];
151  if ($pageType) {
152  $typolinkConfiguration['parameter'] .= ',' . $pageType;
153  }
154  if ($language !== null) {
155  $typolinkConfiguration['language'] = $language;
156  }
157  if ($noCache) {
158  $typolinkConfiguration['no_cache'] = 1;
159  }
160  if ($section) {
161  $typolinkConfiguration['section'] = $section;
162  }
163  if ($linkAccessRestrictedPages) {
164  $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
165  }
166  $typolinkConfiguration['additionalParams'] = ‪HttpUtility::buildQueryString($additionalParams, '&');
167  if ($absolute) {
168  $typolinkConfiguration['forceAbsoluteUrl'] = true;
169  }
170  if ($addQueryString && $addQueryString !== 'false') {
171  $typolinkConfiguration['addQueryString'] = $addQueryString;
172  if ($argumentsToBeExcludedFromQueryString !== []) {
173  $typolinkConfiguration['addQueryString.']['exclude'] = implode(',', $argumentsToBeExcludedFromQueryString);
174  }
175  }
176 
177  try {
178  $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
179  $cObj->setRequest($request);
180  $linkFactory = GeneralUtility::makeInstance(LinkFactory::class);
181  $linkResult = $linkFactory->create((string)$renderChildrenClosure(), $typolinkConfiguration, $cObj);
182  return $linkResult->getUrl();
183  } catch (‪UnableToLinkException) {
184  return (string)$renderChildrenClosure();
185  }
186  }
187 
188  protected static function ‪renderWithExtbaseContext(ExtbaseRequestInterface $request, array $arguments): string
189  {
190  $pageUid = (int)($arguments['pageUid'] ?? 0);
191  $pageType = (int)($arguments['pageType'] ?? 0);
192  $noCache = (bool)($arguments['noCache'] ?? false);
194  $language = isset($arguments['language']) ? (string)$arguments['language'] : null;
196  $section = $arguments['section'] ?? null;
198  $format = $arguments['format'] ?? null;
199  $linkAccessRestrictedPages = (bool)($arguments['linkAccessRestrictedPages'] ?? false);
201  $additionalParams = $arguments['additionalParams'] ?? null;
202  $absolute = (bool)($arguments['absolute'] ?? false);
204  $addQueryString = $arguments['addQueryString'] ?? false;
206  $argumentsToBeExcludedFromQueryString = $arguments['argumentsToBeExcludedFromQueryString'] ?? null;
208  $action = $arguments['action'] ?? null;
210  $controller = $arguments['controller'] ?? null;
212  $extensionName = $arguments['extensionName'] ?? null;
214  $pluginName = $arguments['pluginName'] ?? null;
216  $arguments = $arguments['arguments'] ?? [];
217 
219  $uriBuilder = GeneralUtility::makeInstance(ExtbaseUriBuilder::class);
220  $uriBuilder->reset();
221  $uriBuilder->setRequest($request);
222 
223  if ($pageUid > 0) {
224  $uriBuilder->setTargetPageUid($pageUid);
225  }
226  if ($pageType > 0) {
227  $uriBuilder->setTargetPageType($pageType);
228  }
229  if ($noCache === true) {
230  $uriBuilder->setNoCache($noCache);
231  }
232  if (is_string($section)) {
233  $uriBuilder->setSection($section);
234  }
235  if (is_string($format)) {
236  $uriBuilder->setFormat($format);
237  }
238  if (is_array($additionalParams)) {
239  $uriBuilder->setArguments($additionalParams);
240  }
241  if ($absolute === true) {
242  $uriBuilder->setCreateAbsoluteUri($absolute);
243  }
244  if ($addQueryString && $addQueryString !== 'false') {
245  $uriBuilder->setAddQueryString($addQueryString);
246  }
247  if (is_array($argumentsToBeExcludedFromQueryString)) {
248  $uriBuilder->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString);
249  }
250  if ($linkAccessRestrictedPages === true) {
251  $uriBuilder->setLinkAccessRestrictedPages(true);
252  }
253 
254  $uriBuilder->setLanguage($language);
255 
256  return $uriBuilder->uriFor($action, $arguments, $controller, $extensionName, $pluginName);
257  }
258 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper\renderWithExtbaseContext
‪static renderWithExtbaseContext(ExtbaseRequestInterface $request, array $arguments)
Definition: ActionViewHelper.php:187
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper\initializeArguments
‪initializeArguments()
Definition: ActionViewHelper.php:51
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ActionViewHelper.php:71
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper\renderFrontendLinkWithCoreContext
‪static renderFrontendLinkWithCoreContext(ServerRequestInterface $request, array $arguments, \Closure $renderChildrenClosure)
Definition: ActionViewHelper.php:88
‪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\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:18
‪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\Fluid\ViewHelpers\Uri\ActionViewHelper
Definition: ActionViewHelper.php:49
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55