Passed
Push — master ( 62e46d...e4833f )
by
unknown
15:08
created

getRouteIdentifierForModuleName()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 4
nc 3
nop 1
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\Button;
19
20
use TYPO3\CMS\Backend\Routing\Router;
21
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
22
use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Object\ObjectManager;
25
use TYPO3\CMS\Extbase\Service\ExtensionService;
26
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27
28
/**
29
 * A ViewHelper for adding a shortcut button to the doc header area.
30
 * It must be a child of :ref:`<be:moduleLayout> <typo3-backend-modulelayout>`.
31
 *
32
 * The 'arguments' argument should contain key/value pairs of all arguments
33
 * relevant for the specific view.
34
 *
35
 * Examples
36
 * --------
37
 *
38
 * Default::
39
 *
40
 *    <be:moduleLayout>
41
 *        <be:moduleLayout.button.shortcutButton displayName="Shortcut label" arguments="{parameter: '{someValue}'}"/>
42
 *    </be:moduleLayout>
43
 */
44
class ShortcutButtonViewHelper extends AbstractButtonViewHelper
45
{
46
    /**
47
     * Initialize arguments.
48
     *
49
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
50
     */
51
    public function initializeArguments(): void
52
    {
53
        parent::initializeArguments();
54
        // This will be required in v12. Deprecation for empty argument logged by ModuleTemplate->makeShortcutIcon()
55
        $this->registerArgument('displayName', 'string', 'Name for the shortcut', false, '');
56
        $this->registerArgument('arguments', 'array', 'List of relevant GET variables as key/values list to store', false, []);
57
        // @deprecated since v11, will be removed in v12. Use 'arguments' instead. Deprecation logged by ModuleTemplate->makeShortcutIcon()
58
        $this->registerArgument('getVars', 'array', 'List of additional GET variables to store. The current id, module and all module arguments will always be stored', false, []);
59
    }
60
61
    protected static function createButton(ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ButtonInterface
62
    {
63
        $currentRequest = $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

63
        /** @scrutinizer ignore-call */ 
64
        $currentRequest = $renderingContext->getRequest();
Loading history...
64
        $moduleName = $currentRequest->getPluginName();
65
        $displayName = $arguments['displayName'];
66
67
        // Initialize the shortcut button
68
        $shortcutButton = $buttonBar
69
            ->makeShortcutButton()
70
            ->setDisplayName($displayName)
71
            ->setRouteIdentifier(self::getRouteIdentifierForModuleName($moduleName));
72
73
        if (!empty($arguments['arguments'])) {
74
            $shortcutButton->setArguments($arguments['arguments']);
75
        } else {
76
            // @deprecated since v11, will be removed in v12. Use 'variables' instead. Deprecation logged by ModuleTemplate->makeShortcutIcon()
77
            $extensionName = $currentRequest->getControllerExtensionName();
78
            $argumentPrefix = GeneralUtility::makeInstance(ObjectManager::class)
79
                ->get(ExtensionService::class)
80
                ->getPluginNamespace($extensionName, $moduleName);
81
            $getVars = $arguments['getVars'];
82
            $getVars[] = $argumentPrefix;
83
            $shortcutButton->setGetVariables($getVars);
84
        }
85
86
        return $shortcutButton;
87
    }
88
89
    /**
90
     * Tries to fetch the route identifier for a given module name
91
     *
92
     * @param string $moduleName
93
     * @return string
94
     */
95
    protected static function getRouteIdentifierForModuleName(string $moduleName): string
96
    {
97
        foreach (GeneralUtility::makeInstance(Router::class)->getRoutes() as $identifier => $route) {
98
            if ($route->hasOption('moduleName') && $route->getOption('moduleName') === $moduleName) {
99
                return $identifier;
100
            }
101
        }
102
103
        return '';
104
    }
105
}
106