ShortcutButtonViewHelper.php 4.03 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button;

20
use TYPO3\CMS\Backend\Routing\Router;
21
22
23
24
25
26
27
28
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
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
33
34
 * The 'arguments' argument should contain key/value pairs of all arguments
 * relevant for the specific view.
 *
35
36
37
38
39
40
 * Examples
 * --------
 *
 * Default::
 *
 *    <be:moduleLayout>
41
 *        <be:moduleLayout.button.shortcutButton displayName="Shortcut label" arguments="{parameter: '{someValue}'}"/>
42
 *    </be:moduleLayout>
43
44
45
46
47
48
49
50
51
52
53
 */
class ShortcutButtonViewHelper extends AbstractButtonViewHelper
{
    /**
     * Initialize arguments.
     *
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
     */
    public function initializeArguments(): void
    {
        parent::initializeArguments();
54
55
        // This will be required in v12. Deprecation for empty argument logged by ModuleTemplate->makeShortcutIcon()
        $this->registerArgument('displayName', 'string', 'Name for the shortcut', false, '');
56
57
        $this->registerArgument('arguments', 'array', 'List of relevant GET variables as key/values list to store', false, []);
        // @deprecated since v11, will be removed in v12. Use 'arguments' instead. Deprecation logged by ModuleTemplate->makeShortcutIcon()
58
59
60
61
62
        $this->registerArgument('getVars', 'array', 'List of additional GET variables to store. The current id, module and all module arguments will always be stored', false, []);
    }

    protected static function createButton(ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ButtonInterface
    {
63
        $currentRequest = $renderingContext->getRequest();
64
        $moduleName = $currentRequest->getPluginName();
65
        $displayName = $arguments['displayName'];
66

67
68
69
        // Initialize the shortcut button
        $shortcutButton = $buttonBar
            ->makeShortcutButton()
70
            ->setDisplayName($displayName)
71
            ->setRouteIdentifier(self::getRouteIdentifierForModuleName($moduleName));
72

73
74
75
76
77
78
79
80
81
82
83
84
85
        if (!empty($arguments['arguments'])) {
            $shortcutButton->setArguments($arguments['arguments']);
        } else {
            // @deprecated since v11, will be removed in v12. Use 'variables' instead. Deprecation logged by ModuleTemplate->makeShortcutIcon()
            $extensionName = $currentRequest->getControllerExtensionName();
            $argumentPrefix = GeneralUtility::makeInstance(ObjectManager::class)
                ->get(ExtensionService::class)
                ->getPluginNamespace($extensionName, $moduleName);
            $getVars = $arguments['getVars'];
            $getVars[] = $argumentPrefix;
            $shortcutButton->setGetVariables($getVars);
        }

86
87
        return $shortcutButton;
    }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    /**
     * Tries to fetch the route identifier for a given module name
     *
     * @param string $moduleName
     * @return string
     */
    protected static function getRouteIdentifierForModuleName(string $moduleName): string
    {
        foreach (GeneralUtility::makeInstance(Router::class)->getRoutes() as $identifier => $route) {
            if ($route->hasOption('moduleName') && $route->getOption('moduleName') === $moduleName) {
                return $identifier;
            }
        }

        return '';
    }
105
}