Passed
Push — master ( 4a1204...a560b9 )
by
unknown
15:38
created

ModuleLayoutViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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;
19
20
use TYPO3\CMS\Backend\Template\ModuleTemplate;
21
use TYPO3\CMS\Core\Messaging\FlashMessageService;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Service\ExtensionService;
24
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27
use TYPO3Fluid\Fluid\View\Exception;
28
29
/**
30
 * A ViewHelper for having properly styled backend modules.
31
 * It is recommended to use it in Fluid Layouts.
32
 * It will render the required HTML for the doc header.
33
 * All module specific output and further configuration of the doc header
34
 * must be rendered as children of this ViewHelper.
35
 *
36
 * Examples
37
 * ========
38
 *
39
 * Default::
40
 *
41
 *    <be:moduleLayout>
42
 *       <f:render section="content" />
43
 *    </be:moduleLayout>
44
 *
45
 * Output::
46
 *
47
 *    <!-- HTML of the backend module -->
48
 */
49
class ModuleLayoutViewHelper extends AbstractViewHelper
50
{
51
    use CompileWithRenderStatic;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $escapeOutput = false;
57
58
    /**
59
     * Initialize arguments.
60
     *
61
     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
62
     */
63
    public function initializeArguments()
64
    {
65
        parent::initializeArguments();
66
        $this->registerArgument('name', 'string', 'Name of the module, defaults to the current plugin name, if available', false);
67
    }
68
69
    public static function renderStatic(
70
        array $arguments,
71
        \Closure $renderChildrenClosure,
72
        RenderingContextInterface $renderingContext
73
    ) {
74
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
75
        if ($viewHelperVariableContainer->exists(self::class, ModuleTemplate::class)) {
76
            throw new Exception('ModuleLayoutViewHelper can only be used once per module.', 1483292643);
77
        }
78
79
        $extensionService = GeneralUtility::makeInstance(ExtensionService::class);
80
        $pluginNamespace = $extensionService->getPluginNamespace(
81
            $renderingContext->getRequest()->getControllerExtensionName(),
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

81
            $renderingContext->/** @scrutinizer ignore-call */ 
82
                               getRequest()->getControllerExtensionName(),
Loading history...
82
            $renderingContext->getRequest()->getPluginName()
83
        );
84
85
        $flashMessageQueue = GeneralUtility::makeInstance(FlashMessageService::class)
86
            ->getMessageQueueByIdentifier('extbase.flashmessages.' . $pluginNamespace);
87
88
        $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
89
        $moduleTemplate->setFlashMessageQueue($flashMessageQueue);
90
        if (($arguments['name'] ?? null) !== null) {
91
            $moduleTemplate->setModuleName($arguments['name']);
92
        }
93
94
        $viewHelperVariableContainer->add(self::class, ModuleTemplate::class, $moduleTemplate);
95
        $moduleTemplate->setContent($renderChildrenClosure());
96
        $viewHelperVariableContainer->remove(self::class, ModuleTemplate::class);
97
98
        return $moduleTemplate->renderContent();
99
    }
100
}
101