1<?php
2namespace TYPO3\CMS\Fluid\ViewHelpers\Be;
3
4/*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17use TYPO3\CMS\Core\Page\PageRenderer;
18use TYPO3\CMS\Core\Utility\GeneralUtility;
19use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24/**
25 * ViewHelper which allows you to create extbase based modules in the style of TYPO3 default modules.
26 *
27 * Examples
28 * ========
29 *
30 * All options::
31 *
32 *    <f:be.pageRenderer pageTitle="foo"
33 *        includeCssFiles="{0: '{f:uri.resource(path:\'Css/Styles.css\')}'}"
34 *        includeJsFiles="{0: '{f:uri.resource(path:\'JavaScript/Library1.js\')}', 1: '{f:uri.resource(path:\'JavaScript/Library2.js\')}'}"
35 *        addJsInlineLabels="{0: 'label1', 1: 'label2'}" />
36 *
37 * Custom CSS file :file:`EXT:your_extension/Resources/Public/Css/styles.css` and
38 * JavaScript files :file:`EXT:your_extension/Resources/Public/JavaScript/Library1.js` and
39 * :file:`EXT:your_extension/Resources/Public/JavaScript/Library2.js`
40 * will be loaded, plus some inline labels for usage in JS code.
41 */
42class PageRendererViewHelper extends AbstractViewHelper
43{
44    use CompileWithRenderStatic;
45
46    /**
47     * Initialize arguments.
48     *
49     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
50     */
51    public function initializeArguments()
52    {
53        $this->registerArgument('pageTitle', 'string', 'title tag of the module. Not required by default, as BE modules are shown in a frame', false, '');
54        $this->registerArgument('includeCssFiles', 'array', 'List of custom CSS file to be loaded');
55        $this->registerArgument('includeJsFiles', 'array', 'List of custom JavaScript file to be loaded');
56        $this->registerArgument('addJsInlineLabels', 'array', 'Custom labels to add to JavaScript inline labels');
57        $this->registerArgument('includeRequireJsModules', 'array', 'List of RequireJS modules to be loaded');
58        $this->registerArgument('addInlineSettings', 'array', 'Adds Javascript Inline Setting');
59    }
60
61    /**
62     * @param array $arguments
63     * @param \Closure $renderChildrenClosure
64     * @param RenderingContextInterface $renderingContext
65     */
66    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
67    {
68        $pageRenderer = static::getPageRenderer();
69        $pageTitle = $arguments['pageTitle'];
70        $includeCssFiles = $arguments['includeCssFiles'];
71        $includeJsFiles = $arguments['includeJsFiles'];
72        $addJsInlineLabels = $arguments['addJsInlineLabels'];
73        $includeRequireJsModules = $arguments['includeRequireJsModules'];
74        $addInlineSettings = $arguments['addInlineSettings'];
75
76        if ($pageTitle) {
77            $pageRenderer->setTitle($pageTitle);
78        }
79
80        // Include custom CSS and JS files
81        if (is_array($includeCssFiles) && count($includeCssFiles) > 0) {
82            foreach ($includeCssFiles as $addCssFile) {
83                $pageRenderer->addCssFile($addCssFile);
84            }
85        }
86        if (is_array($includeJsFiles) && count($includeJsFiles) > 0) {
87            foreach ($includeJsFiles as $addJsFile) {
88                $pageRenderer->addJsFile($addJsFile);
89            }
90        }
91        if (is_array($includeRequireJsModules) && count($includeRequireJsModules) > 0) {
92            foreach ($includeRequireJsModules as $addRequireJsFile) {
93                $pageRenderer->loadRequireJsModule($addRequireJsFile);
94            }
95        }
96
97        if (is_array($addInlineSettings) && count($addInlineSettings) > 0) {
98            $pageRenderer->addInlineSettingArray(null, $addInlineSettings);
99        }
100
101        // Add inline language labels
102        if (is_array($addJsInlineLabels) && count($addJsInlineLabels) > 0) {
103            $extensionKey = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionKey();
104            foreach ($addJsInlineLabels as $key) {
105                $label = LocalizationUtility::translate($key, $extensionKey);
106                $pageRenderer->addInlineLanguageLabel($key, $label);
107            }
108        }
109    }
110
111    /**
112     * @return PageRenderer
113     */
114    protected static function getPageRenderer()
115    {
116        return GeneralUtility::makeInstance(PageRenderer::class);
117    }
118}
119