Passed
Push — master ( af49bb...0a24d1 )
by
unknown
15:19
created

MenuItemViewHelper::isCurrentUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\ViewHelpers\ModuleLayout;
19
20
use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
21
use TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper;
22
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
26
use TYPO3Fluid\Fluid\View\Exception;
27
28
/**
29
 * A ViewHelper for adding a menu item to a doc header menu.
30
 * It must be a child of :ref:`<be:moduleLayout.menu> <typo3-backend-modulelayout-menu>`.
31
 *
32
 * Examples
33
 * ========
34
 *
35
 * Default::
36
 *
37
 *    <be:moduleLayout>
38
 *        <be:moduleLayout.menu identifier="MenuIdentifier">
39
 *            <be:moduleLayout.menuItem label="Menu item 1" uri="{f:uri.action(action: 'index')}"/>
40
 *        </be:moduleLayout.menu>
41
 *    </be:moduleLayout>
42
 *
43
 * @deprecated since TYPO3 v11.3, will be removed in TYPO3 v12.0.
44
 */
45
class MenuItemViewHelper extends AbstractViewHelper
46
{
47
    use CompileWithRenderStatic;
48
49
    /**
50
     * Initialize arguments.
51
     *
52
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
53
     */
54
    public function initializeArguments()
55
    {
56
        parent::initializeArguments();
57
        $this->registerArgument('label', 'string', 'Label of the menu item', true);
58
        $this->registerArgument('uri', 'string', 'Action uri', true);
59
    }
60
61
    /**
62
     * @param array $arguments
63
     * @param \Closure $renderChildrenClosure
64
     * @param RenderingContextInterface $renderingContext
65
     * @throws Exception
66
     * @throws \InvalidArgumentException
67
     */
68
    public static function renderStatic(
69
        array $arguments,
70
        \Closure $renderChildrenClosure,
71
        RenderingContextInterface $renderingContext
72
    ) {
73
        trigger_error(__CLASS__ . ' will be removed in TYPO3 v12.', E_USER_DEPRECATED);
74
        $request = $renderingContext->getRequest();
0 ignored issues
show
Bug introduced by
The method getRequest() 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

74
        /** @scrutinizer ignore-call */ 
75
        $request = $renderingContext->getRequest();
Loading history...
75
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
76
        self::ensureProperNesting($viewHelperVariableContainer);
77
78
        /** @var Menu $menu */
79
        $menu = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, Menu::class);
80
        $menuItem = $menu->makeMenuItem();
81
        $menuItem->setTitle($arguments['label']);
82
        $menuItem->setHref($arguments['uri']);
83
        $menuItem->setActive($request->getAttribute('normalizedParams')->getRequestUri() === $arguments['uri']);
84
        $menu->addMenuItem($menuItem);
85
    }
86
87
    /**
88
     * @param ViewHelperVariableContainer $viewHelperVariableContainer
89
     * @throws Exception
90
     */
91
    private static function ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
92
    {
93
        if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, Menu::class)) {
94
            throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout.menu> ViewHelper', self::class), 1531235592);
95
        }
96
    }
97
}
98