page link * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-page */ final class PageViewHelper extends AbstractTagBasedViewHelper { /** * @var string */ protected $tagName = 'a'; public function __construct( private readonly BackendUriBuilder $uriBuilder, private readonly LinkFactory $linkFactory ) { parent::__construct(); } public function initializeArguments(): void { parent::initializeArguments(); $this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination'); $this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter'); $this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.'); $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'); $this->registerArgument('section', 'string', 'The anchor to be added to the URI'); $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.'); $this->registerArgument('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)'); $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute'); $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); $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = true'); } public function render(): string { $request = null; if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) { $request = $this->renderingContext->getAttribute(ServerRequestInterface::class); } if ($request instanceof ExtbaseRequestInterface) { return $this->renderWithExtbaseContext($request); } if ($request instanceof ServerRequestInterface) { if (ApplicationType::fromRequest($request)->isFrontend()) { // Use the regular typolink functionality. return $this->renderFrontendLinkWithCoreContext($request); } $uri = $this->renderBackendLinkWithCoreContext($request); if ($uri !== '') { $this->tag->addAttribute('href', $uri); $this->tag->setContent((string)$this->renderChildren()); $this->tag->forceClosingTag(true); $result = $this->tag->render(); } else { $result = (string)$this->renderChildren(); } return $result; } throw new \RuntimeException( 'The rendering context of ViewHelper f:link.page is missing a valid request object.', 1639819269 ); } private function renderFrontendLinkWithCoreContext(ServerRequestInterface $request): string { $pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : 'current'; $pageType = isset($this->arguments['pageType']) ? (int)$this->arguments['pageType'] : 0; $noCache = isset($this->arguments['noCache']) && (bool)$this->arguments['noCache']; $section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : ''; $language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null; $linkAccessRestrictedPages = isset($this->arguments['linkAccessRestrictedPages']) && (bool)$this->arguments['linkAccessRestrictedPages']; $additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : []; $absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute']; $addQueryString = $this->arguments['addQueryString'] ?? false; $argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : []; $typolinkConfiguration = [ 'parameter' => $pageUid, ]; if ($pageType) { $typolinkConfiguration['parameter'] .= ',' . $pageType; } if ($noCache) { $typolinkConfiguration['no_cache'] = 1; } if ($language !== null) { $typolinkConfiguration['language'] = $language; } if ($section) { $typolinkConfiguration['section'] = $section; } if ($linkAccessRestrictedPages) { $typolinkConfiguration['linkAccessRestrictedPages'] = 1; } if ($additionalParams) { $typolinkConfiguration['queryParameters'] = $additionalParams; } if ($absolute) { $typolinkConfiguration['forceAbsoluteUrl'] = true; } if ($addQueryString && $addQueryString !== 'false') { $typolinkConfiguration['addQueryString'] = $addQueryString; if ($argumentsToBeExcludedFromQueryString !== []) { $typolinkConfiguration['addQueryString.']['exclude'] = implode(',', $argumentsToBeExcludedFromQueryString); } } try { $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class); $cObj->setRequest($request); $linkResult = $this->linkFactory->create((string)$this->renderChildren(), $typolinkConfiguration, $cObj); // Removing TypoLink target here to ensure same behaviour with extbase uri builder in this context. $linkResultAttributes = $linkResult->getAttributes(); unset($linkResultAttributes['target']); $this->tag->addAttributes($linkResultAttributes); $this->tag->setContent((string)$this->renderChildren()); $this->tag->forceClosingTag(true); $result = $this->tag->render(); } catch (UnableToLinkException) { $result = (string)$this->renderChildren(); } return $result; } private function renderBackendLinkWithCoreContext(ServerRequestInterface $request): string { $pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : null; $section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : ''; $additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : []; $absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute']; $addQueryString = $this->arguments['addQueryString'] ?? false; $argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : []; $arguments = []; if ($addQueryString && $addQueryString !== 'false') { $arguments = $request->getQueryParams(); foreach ($argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) { $argumentArrayToBeExcluded = []; parse_str($argumentToBeExcluded, $argumentArrayToBeExcluded); $arguments = ArrayUtility::arrayDiffKeyRecursive($arguments, $argumentArrayToBeExcluded); } } $id = $pageUid ?? $request->getQueryParams()['id'] ?? null; if ($id !== null) { $arguments['id'] = $id; } if (!isset($arguments['route']) && ($route = $request->getAttribute('route')) instanceof Route) { $arguments['route'] = $route->getOption('_identifier'); } $arguments = array_replace_recursive($arguments, $additionalParams); $routeName = $arguments['route'] ?? null; unset($arguments['route'], $arguments['token']); try { if ($absolute) { $uri = (string)$this->uriBuilder->buildUriFromRoute($routeName, $arguments, BackendUriBuilder::ABSOLUTE_URL); } else { $uri = (string)$this->uriBuilder->buildUriFromRoute($routeName, $arguments, BackendUriBuilder::ABSOLUTE_PATH); } } catch (RouteNotFoundException) { $uri = ''; } if ($section !== '') { $uri .= '#' . $section; } return $uri; } private function renderWithExtbaseContext(ExtbaseRequestInterface $request): string { $pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : null; $pageType = isset($this->arguments['pageType']) ? (int)$this->arguments['pageType'] : 0; $noCache = isset($this->arguments['noCache']) && (bool)$this->arguments['noCache']; $section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : ''; $language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null; $linkAccessRestrictedPages = isset($this->arguments['linkAccessRestrictedPages']) && (bool)$this->arguments['linkAccessRestrictedPages']; $additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : []; $absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute']; $addQueryString = $this->arguments['addQueryString'] ?? false; $argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : []; $uriBuilder = GeneralUtility::makeInstance(ExtbaseUriBuilder::class); $uriBuilder->reset() ->setRequest($request) ->setTargetPageType($pageType) ->setNoCache($noCache) ->setSection($section) ->setLanguage($language) ->setLinkAccessRestrictedPages($linkAccessRestrictedPages) ->setArguments($additionalParams) ->setCreateAbsoluteUri($absolute) ->setAddQueryString($addQueryString) ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString); if (MathUtility::canBeInterpretedAsInteger($pageUid)) { $uriBuilder->setTargetPageUid((int)$pageUid); } $uri = $uriBuilder->build(); if ($uri !== '') { $this->tag->addAttribute('href', $uri); $this->tag->setContent((string)$this->renderChildren()); $this->tag->forceClosingTag(true); $result = $this->tag->render(); } else { $result = (string)$this->renderChildren(); } return $result; } }