1<?php
2
3declare(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
18namespace TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button;
19
20use TYPO3\CMS\Backend\Routing\Router;
21use TYPO3\CMS\Backend\Template\Components\ButtonBar;
22use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
23use TYPO3\CMS\Core\Utility\GeneralUtility;
24use TYPO3\CMS\Extbase\Service\ExtensionService;
25use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26
27/**
28 * A ViewHelper for adding a shortcut button to the doc header area.
29 * It must be a child of :ref:`<be:moduleLayout> <typo3-backend-modulelayout>`.
30 *
31 * The 'arguments' argument should contain key/value pairs of all arguments
32 * relevant for the specific view.
33 *
34 * Examples
35 * --------
36 *
37 * Default::
38 *
39 *    <be:moduleLayout>
40 *        <be:moduleLayout.button.shortcutButton displayName="Shortcut label" arguments="{parameter: '{someValue}'}"/>
41 *    </be:moduleLayout>
42 *
43 * @deprecated since TYPO3 v11.3, will be removed in TYPO3 v12.0. Deprecation logged AbstractButtonViewHelper.
44 */
45class ShortcutButtonViewHelper extends AbstractButtonViewHelper
46{
47    /**
48     * Initialize arguments.
49     *
50     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
51     */
52    public function initializeArguments(): void
53    {
54        parent::initializeArguments();
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        $this->registerArgument('getVars', 'array', 'List of additional GET variables to store. The current id, module and all module arguments will always be stored', false, []);
58    }
59
60    protected static function createButton(ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ButtonInterface
61    {
62        $currentRequest = $renderingContext->getRequest();
63        $moduleName = $currentRequest->getPluginName();
64        $displayName = $arguments['displayName'];
65
66        // Initialize the shortcut button
67        $shortcutButton = $buttonBar
68            ->makeShortcutButton()
69            ->setDisplayName($displayName)
70            ->setRouteIdentifier(self::getRouteIdentifierForModuleName($moduleName));
71
72        if (!empty($arguments['arguments'])) {
73            $shortcutButton->setArguments($arguments['arguments']);
74        } else {
75            $extensionName = $currentRequest->getControllerExtensionName();
76            $argumentPrefix = GeneralUtility::makeInstance(ExtensionService::class)
77                ->getPluginNamespace($extensionName, $moduleName);
78            $getVars = $arguments['getVars'];
79            $getVars[] = $argumentPrefix;
80            $shortcutButton->setGetVariables($getVars);
81        }
82
83        return $shortcutButton;
84    }
85
86    /**
87     * Tries to fetch the route identifier for a given module name
88     *
89     * @param string $moduleName
90     * @return string
91     */
92    protected static function getRouteIdentifierForModuleName(string $moduleName): string
93    {
94        foreach (GeneralUtility::makeInstance(Router::class)->getRoutes() as $identifier => $route) {
95            if ($route->hasOption('moduleName') && $route->getOption('moduleName') === $moduleName) {
96                return $identifier;
97            }
98        }
99
100        return '';
101    }
102}
103