Completed
Push — master ( 27877c...f9df4a )
by
unknown
17:40
created

evaluateSelectItemState()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 17
rs 9.8666
1
<?php
2
namespace TYPO3\CMS\Fluid\ViewHelpers\Be\Menus;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\ArrayUtility;
18
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
19
20
/**
21
 * ViewHelper which returns an option tag.
22
 * This ViewHelper only works in conjunction with :php:`\TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuViewHelper`.
23
 *
24
 * .. note::
25
 *    This ViewHelper is experimental!
26
 *
27
 * Examples
28
 * ========
29
 *
30
 * Simple::
31
 *
32
 *    <f:be.menus.actionMenu>
33
 *       <f:be.menus.actionMenuItem label="Overview" controller="Blog" action="index" />
34
 *       <f:be.menus.actionMenuItem label="Create new Blog" controller="Blog" action="new" />
35
 *       <f:be.menus.actionMenuItem label="List Posts" controller="Post" action="index" arguments="{blog: blog}" />
36
 *    </f:be.menus.actionMenu>
37
 *
38
 * Selectbox with the options "Overview", "Create new Blog" and "List Posts".
39
 *
40
 * Localized::
41
 *
42
 *    <f:be.menus.actionMenu>
43
 *       <f:be.menus.actionMenuItem label="{f:translate(key='overview')}" controller="Blog" action="index" />
44
 *       <f:be.menus.actionMenuItem label="{f:translate(key='create_blog')}" controller="Blog" action="new" />
45
 *    </f:be.menus.actionMenu>
46
 *
47
 * Localized selectbox.
48
 */
49
class ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
50
{
51
    /**
52
     * @var string
53
     */
54
    protected $tagName = 'option';
55
56
    /**
57
     * Initialize arguments.
58
     *
59
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
60
     */
61
    public function initializeArguments()
62
    {
63
        parent::initializeArguments();
64
        $this->registerArgument('label', 'string', 'label of the option tag', true);
65
        $this->registerArgument('controller', 'string', 'controller to be associated with this ActionMenuItem', true);
66
        $this->registerArgument('action', 'string', 'the action to be associated with this ActionMenuItem', true);
67
        $this->registerArgument('arguments', 'array', 'additional controller arguments to be passed to the action when this ActionMenuItem is selected', false, []);
68
    }
69
70
    /**
71
     * Renders an ActionMenu option tag
72
     *
73
     * @return string the rendered option tag
74
     * @see \TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuViewHelper
75
     */
76
    public function render()
77
    {
78
        $label = $this->arguments['label'];
79
        $controller = $this->arguments['controller'];
80
        $action = $this->arguments['action'];
81
        $arguments = $this->arguments['arguments'];
82
83
        $uri = $this->renderingContext->getControllerContext()->getUriBuilder()->reset()->uriFor($action, $arguments, $controller);
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

83
        $uri = $this->renderingContext->/** @scrutinizer ignore-call */ getControllerContext()->getUriBuilder()->reset()->uriFor($action, $arguments, $controller);

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...
84
        $this->tag->addAttribute('value', $uri);
85
86
        if (!$this->tag->hasAttribute('selected')) {
87
            $this->evaluateSelectItemState($controller, $action, $arguments);
88
        }
89
90
        $this->tag->setContent($label);
91
        return $this->tag->render();
92
    }
93
94
    protected function evaluateSelectItemState(string $controller, string $action, array $arguments): void
95
    {
96
        $currentRequest = $this->renderingContext->getControllerContext()->getRequest();
97
        $flatRequestArguments = ArrayUtility::flatten(
98
            array_merge([
99
                'controller' => $currentRequest->getControllerName(),
100
                'action' => $currentRequest->getControllerActionName()
101
            ], $currentRequest->getArguments())
102
        );
103
        $flatViewHelperArguments = ArrayUtility::flatten(
104
            array_merge(['controller' => $controller, 'action' => $action], $arguments)
105
        );
106
        if (
107
            $this->arguments['selected'] ||
108
            array_diff($flatRequestArguments, $flatViewHelperArguments) === []
109
        ) {
110
            $this->tag->addAttribute('selected', 'selected');
111
        }
112
    }
113
}
114