Completed
Push — master ( c1d8f4...533b50 )
by
unknown
29:29
created

LinkViewHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 12 2
A getAjaxUri() 0 13 2
A initializeArguments() 0 14 1
A getWidgetUri() 0 26 6
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\Widget;
17
18
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
19
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20
21
/**
22
 * A ViewHelper for creating Links to Extbase actions within widgets.
23
 *
24
 * Examples
25
 * ========
26
 *
27
 * URI to the show-action of the current controller::
28
 *
29
 *    <f:widget.link action="show">link</f:widget.link>
30
 *
31
 * Output::
32
 *
33
 *    <a href="index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz">link</a>
34
 *
35
 * Depending on current page, routing and page path configuration.
36
 */
37
class LinkViewHelper extends AbstractTagBasedViewHelper
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $tagName = 'a';
43
44
    /**
45
     * Initialize arguments
46
     */
47
    public function initializeArguments()
48
    {
49
        parent::initializeArguments();
50
        $this->registerUniversalTagAttributes();
51
        $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
52
        $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
53
        $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
54
        $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
55
        $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
56
        $this->registerArgument('action', 'string', 'Target action');
57
        $this->registerArgument('arguments', 'array', 'Arguments', false, []);
58
        $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
59
        $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
60
        $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
61
    }
62
63
    /**
64
     * Render the link.
65
     *
66
     * @return string The rendered link
67
     */
68
    public function render()
69
    {
70
        $ajax = $this->arguments['ajax'];
71
72
        if ($ajax === true) {
73
            $uri = $this->getAjaxUri();
74
        } else {
75
            $uri = $this->getWidgetUri();
76
        }
77
        $this->tag->addAttribute('href', $uri);
78
        $this->tag->setContent($this->renderChildren());
79
        return $this->tag->render();
80
    }
81
82
    /**
83
     * Get the URI for an AJAX Request.
84
     *
85
     * @return string the AJAX URI
86
     */
87
    protected function getAjaxUri()
88
    {
89
        $action = $this->arguments['action'];
90
        $arguments = $this->arguments['arguments'];
91
        if ($action === null) {
92
            $action = $this->renderingContext->getControllerContext()->getRequest()->getControllerActionName();
0 ignored issues
show
Bug introduced by
The method getControllerContext() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. Did you maybe mean getControllerAction()? ( Ignorable by Annotation )

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

92
            $action = $this->renderingContext->/** @scrutinizer ignore-call */ getControllerContext()->getRequest()->getControllerActionName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        }
94
        $arguments['id'] = $GLOBALS['TSFE']->id;
95
        // @todo page type should be configurable
96
        $arguments['type'] = 7076;
97
        $arguments['fluid-widget-id'] = $this->renderingContext->getControllerContext()->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
98
        $arguments['action'] = $action;
99
        return '?' . http_build_query($arguments, null, '&');
100
    }
101
102
    /**
103
     * Get the URI for a non-AJAX Request.
104
     *
105
     * @return string the Widget URI
106
     */
107
    protected function getWidgetUri()
108
    {
109
        /** @var UriBuilder $uriBuilder */
110
        $uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
111
        $argumentPrefix = $this->renderingContext->getControllerContext()->getRequest()->getArgumentPrefix();
112
        $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
113
        if ($this->hasArgument('action')) {
114
            $arguments['action'] = $this->arguments['action'];
115
        }
116
        if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
117
            $arguments['format'] = $this->arguments['format'];
118
        }
119
        $uriBuilder->reset()
120
            ->setArguments([$argumentPrefix => $arguments])
121
            ->setSection($this->arguments['section'])
122
            ->setAddQueryString(true)
123
            ->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
124
            ->setFormat($this->arguments['format'])
125
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ");" but found ")
;"
Loading history...
126
127
        $addQueryStringMethod = $this->arguments['addQueryStringMethod'] ?? null;
128
        if (is_string($addQueryStringMethod)) {
129
            $uriBuilder->setAddQueryStringMethod($addQueryStringMethod);
130
        }
131
132
        return $uriBuilder->build();
133
    }
134
}
135