1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
17
18use TYPO3\CMS\Core\Utility\MathUtility;
19use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
20use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24/**
25 * A ViewHelper for creating URIs to TYPO3 pages.
26 *
27 * Examples
28 * ========
29 *
30 * URI to the current page
31 * -----------------------
32 *
33 * ::
34 *
35 *    <f:uri.page>page link</f:uri.page>
36 *
37 * ``/page/path/name.html``
38 *
39 * Depending on current page, routing and page path configuration.
40 *
41 * Query parameters
42 * ----------------
43 *
44 * ::
45 *
46 *    <f:uri.page pageUid="1" additionalParams="{foo: 'bar'}" />
47 *
48 * ``/page/path/name.html?foo=bar``
49 *
50 * Depending on current page, routing and page path configuration.
51 *
52 * Query parameters for extensions
53 * -------------------------------
54 *
55 * ::
56 *
57 *    <f:uri.page pageUid="1" additionalParams="{extension_key: {foo: 'bar'}}" />
58 *
59 * ``/page/path/name.html?extension_key[foo]=bar``
60 *
61 * Depending on current page, routing and page path configuration.
62 */
63class PageViewHelper extends AbstractViewHelper
64{
65    use CompileWithRenderStatic;
66
67    /**
68     * Initialize arguments
69     */
70    public function initializeArguments()
71    {
72        $this->registerArgument('pageUid', 'int', 'target PID');
73        $this->registerArgument('additionalParams', 'array', 'query parameters to be attached to the resulting URI', false, []);
74        $this->registerArgument('pageType', 'int', 'type of the target page. See typolink.parameter', false, 0);
75        $this->registerArgument('noCache', 'bool', 'set this to disable caching for the target page. You should not need this.', false, false);
76        $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, null);
77        // @deprecated
78        $this->registerArgument('noCacheHash', 'bool', 'Deprecated: Set this to suppress the cHash query parameter created by TypoLink. You should not need this.', false, null);
79        $this->registerArgument('section', 'string', 'the anchor to be added to the URI', false, '');
80        $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);
81        $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false, false);
82        $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URI', false, false);
83        $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
84        $this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE');
85    }
86
87    /**
88     * @param array $arguments
89     * @param \Closure $renderChildrenClosure
90     * @param RenderingContextInterface $renderingContext
91     * @return string Rendered page URI
92     */
93    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
94    {
95        $pageUid = $arguments['pageUid'];
96        $additionalParams = $arguments['additionalParams'];
97        $pageType = $arguments['pageType'];
98        $noCache = $arguments['noCache'];
99        if (isset($arguments['noCacheHash'])) {
100            trigger_error('Using the argument "noCacheHash" in <f:uri.page> ViewHelper has no effect anymore. Remove the argument in your fluid template, as it will result in a fatal error.', E_USER_DEPRECATED);
101        }
102        $section = $arguments['section'];
103        $language = $arguments['language'] ?? null;
104        $linkAccessRestrictedPages = $arguments['linkAccessRestrictedPages'];
105        $absolute = $arguments['absolute'];
106        $addQueryString = $arguments['addQueryString'];
107        $argumentsToBeExcludedFromQueryString = $arguments['argumentsToBeExcludedFromQueryString'];
108        $addQueryStringMethod = $arguments['addQueryStringMethod'];
109
110        /** @var UriBuilder $uriBuilder */
111        $uriBuilder = $renderingContext->getControllerContext()->getUriBuilder();
112        $uri = $uriBuilder
113            ->reset()
114            ->setTargetPageType($pageType)
115            ->setNoCache($noCache)
116            ->setSection($section)
117            ->setLanguage($language)
118            ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
119            ->setArguments($additionalParams)
120            ->setCreateAbsoluteUri($absolute)
121            ->setAddQueryString($addQueryString)
122            ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
123        ;
124
125        if (MathUtility::canBeInterpretedAsInteger($pageUid)) {
126            $uriBuilder->setTargetPageUid((int)$pageUid);
127        }
128
129        if (is_string($addQueryStringMethod)) {
130            $uriBuilder->setAddQueryStringMethod($addQueryStringMethod);
131        }
132
133        return $uri->build();
134    }
135}
136