Passed
Push — master ( d66d7f...a7bade )
by
unknown
18:08
created

ActionViewHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 62
c 0
b 0
f 0
dl 0
loc 86
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 24 1
A render() 0 47 5
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 TYPO3\CMS\Fluid\ViewHelpers\Link;
17
18
use TYPO3\CMS\Core\Utility\MathUtility;
19
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
22
/**
23
 * A ViewHelper for creating links to extbase actions.
24
 *
25
 * Examples
26
 * ========
27
 *
28
 * link to the show-action of the current controller::
29
 *
30
 *    <f:link.action action="show">action link</f:link.action>
31
 *
32
 * Output::
33
 *
34
 *    <a href="index.php?id=123&tx_myextension_plugin[action]=show&tx_myextension_plugin[controller]=Standard&cHash=xyz">action link</a>
35
 *
36
 * Depending on the current page and your TypoScript configuration.
37
 */
38
class ActionViewHelper extends AbstractTagBasedViewHelper
39
{
40
    /**
41
     * @var string
42
     */
43
    protected $tagName = 'a';
44
45
    /**
46
     * Arguments initialization
47
     */
48
    public function initializeArguments()
49
    {
50
        parent::initializeArguments();
51
        $this->registerUniversalTagAttributes();
52
        $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
53
        $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
54
        $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
55
        $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
56
        $this->registerArgument('action', 'string', 'Target action');
57
        $this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used');
58
        $this->registerArgument('extensionName', 'string', 'Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used');
59
        $this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
60
        $this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
61
        $this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter');
62
        $this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.');
63
        $this->registerArgument('section', 'string', 'The anchor to be added to the URI');
64
        $this->registerArgument('format', 'string', 'The requested format, e.g. ".html');
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.');
66
        $this->registerArgument('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)');
67
        $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute');
68
        $this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URI');
69
        $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = TRUE');
70
        $this->registerArgument('addQueryStringMethod', 'string', 'This argument is not evaluated anymore and will be removed in TYPO3 v12.');
71
        $this->registerArgument('arguments', 'array', 'Arguments for the controller action, associative array');
72
    }
73
74
    /**
75
     * @return string Rendered link
76
     */
77
    public function render()
78
    {
79
        if (isset($this->arguments['addQueryStringMethod'])) {
80
            trigger_error('Using the argument "addQueryStringMethod" in <f:link.action> ViewHelper has no effect anymore and will be removed in TYPO3 v12. Remove the argument in your fluid template, as it will result in a fatal error.', E_USER_DEPRECATED);
81
        }
82
        $action = $this->arguments['action'];
83
        $controller = $this->arguments['controller'];
84
        $extensionName = $this->arguments['extensionName'];
85
        $pluginName = $this->arguments['pluginName'];
86
        $pageUid = (int)$this->arguments['pageUid'] ?: null;
87
        $pageType = (int)$this->arguments['pageType'];
88
        $noCache = (bool)$this->arguments['noCache'];
89
        $section = (string)$this->arguments['section'];
90
        $format = (string)$this->arguments['format'];
91
        $linkAccessRestrictedPages = (bool)$this->arguments['linkAccessRestrictedPages'];
92
        $additionalParams = (array)$this->arguments['additionalParams'];
93
        $absolute = (bool)$this->arguments['absolute'];
94
        $addQueryString = (bool)$this->arguments['addQueryString'];
95
        $argumentsToBeExcludedFromQueryString = (array)$this->arguments['argumentsToBeExcludedFromQueryString'];
96
        $parameters = $this->arguments['arguments'];
97
        /** @var UriBuilder $uriBuilder */
98
        $uriBuilder = $this->renderingContext->getUriBuilder();
0 ignored issues
show
Bug introduced by
The method getUriBuilder() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\Core\Re...nderingContextInterface such as TYPO3\CMS\Fluid\Core\Rendering\RenderingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        /** @scrutinizer ignore-call */ 
99
        $uriBuilder = $this->renderingContext->getUriBuilder();
Loading history...
99
        $uriBuilder
100
            ->reset()
101
            ->setTargetPageType($pageType)
102
            ->setNoCache($noCache)
103
            ->setSection($section)
104
            ->setFormat($format)
105
            ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
106
            ->setArguments($additionalParams)
107
            ->setCreateAbsoluteUri($absolute)
108
            ->setAddQueryString($addQueryString)
109
            ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
110
        ;
111
112
        if (MathUtility::canBeInterpretedAsInteger($pageUid)) {
113
            $uriBuilder->setTargetPageUid((int)$pageUid);
114
        }
115
116
        $uri = $uriBuilder->uriFor($action, $parameters, $controller, $extensionName, $pluginName);
117
        if ($uri === '') {
118
            return $this->renderChildren();
119
        }
120
        $this->tag->addAttribute('href', $uri);
121
        $this->tag->setContent($this->renderChildren());
122
        $this->tag->forceClosingTag(true);
123
        return $this->tag->render();
124
    }
125
}
126