Passed
Push — main ( f98caf...46400b )
by Torben
71:32 queued 29:51
created

PageViewHelper::renderStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 10
rs 10
c 1
b 0
f 0
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
16
namespace DERHANSEN\SfEventMgt\ViewHelpers\Uri;
17
18
use Closure;
19
use RuntimeException;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Utility\MathUtility;
22
use TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
23
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder as ExtbaseUriBuilder;
24
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
25
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
27
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28
29
/**
30
 * Modified version of TYPO3 f:uri.page viewHelper, which always generates frontend URLs, so views created in
31
 * backend context always render links in frontend context. Should be used in custom notifications sent using the
32
 * backend module.
33
 */
34
class PageViewHelper extends AbstractViewHelper
35
{
36
    use CompileWithRenderStatic;
37
38
    public function initializeArguments(): void
39
    {
40
        $this->registerArgument('pageUid', 'int', 'target PID');
41
        $this->registerArgument('additionalParams', 'array', 'query parameters to be attached to the resulting URI', false, []);
42
        $this->registerArgument('pageType', 'int', 'type of the target page. See typolink.parameter', false, 0);
43
        $this->registerArgument('noCache', 'bool', 'set this to disable caching for the target page. You should not need this.', false, false);
44
        $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);
45
        $this->registerArgument('section', 'string', 'the anchor to be added to the URI', false, '');
46
        $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);
47
        $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false, false);
48
        $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URI', false, false);
49
        $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
50
    }
51
52
    public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
53
    {
54
        /** @var RenderingContext $renderingContext */
55
        $request = $renderingContext->getRequest();
56
        if ($request instanceof ExtbaseRequestInterface) {
57
            return self::renderWithExtbaseContext($request, $arguments);
58
        }
59
        throw new RuntimeException(
60
            'ViewHelper f:uri.page can be used only in extbase context and needs a request implementing extbase RequestInterface.',
61
            1639820200
62
        );
63
    }
64
65
    protected static function renderWithExtbaseContext(ExtbaseRequestInterface $request, array $arguments): string
66
    {
67
        $pageUid = $arguments['pageUid'];
68
        $additionalParams = $arguments['additionalParams'];
69
        $pageType = $arguments['pageType'];
70
        $noCache = $arguments['noCache'];
71
        $section = $arguments['section'];
72
        $language = $arguments['language'] ?? null;
73
        $linkAccessRestrictedPages = $arguments['linkAccessRestrictedPages'];
74
        $absolute = $arguments['absolute'];
75
        $addQueryString = $arguments['addQueryString'];
76
        $argumentsToBeExcludedFromQueryString = $arguments['argumentsToBeExcludedFromQueryString'];
77
78
        $uriBuilder = GeneralUtility::makeInstance(ExtbaseUriBuilder::class);
79
        $uri = $uriBuilder
80
            ->reset()
81
            ->setRequest($request)
82
            ->setTargetPageType($pageType)
83
            ->setNoCache($noCache)
84
            ->setSection($section)
85
            ->setLanguage($language)
86
            ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
87
            ->setArguments($additionalParams)
88
            ->setCreateAbsoluteUri($absolute)
89
            ->setAddQueryString($addQueryString)
90
            ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString);
91
92
        if (MathUtility::canBeInterpretedAsInteger($pageUid)) {
93
            $uriBuilder->setTargetPageUid((int)$pageUid);
94
        }
95
96
        return $uri->buildFrontendUri();
97
    }
98
}
99