[TASK] Convert remaining ViewHelpers to static calling
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Be / PageRendererViewHelper.php
1 <?php
2 namespace 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
17 use TYPO3\CMS\Core\Page\PageRenderer;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24 /**
25 * View helper which allows you to create extbase based modules in the style of TYPO3 default modules.
26 *
27 * = Examples =
28 *
29 * <code title="All options">
30 * <f:be.pageRenderer pageTitle="foo" includeCssFiles="0: '{f:uri.resource(path:\'Css/Styles.css\')}'" includeJsFiles="0: '{f:uri.resource(path:\'JavaScript/Library1.js\')}', 1: '{f:uri.resource(path:\'JavaScript/Library2.js\')}'" addJsInlineLabels="{0: 'label1', 1: 'label2'}" />
31 * </code>
32 * <output>
33 *
34 * Custom CSS file EXT:your_extension/Resources/Public/Css/styles.css and
35 * JavaScript files EXT:your_extension/Resources/Public/JavaScript/Library1.js and EXT:your_extension/Resources/Public/JavaScript/Library2.js
36 * will be loaded, plus some inline labels for usage in JS code.
37 * </output>
38 */
39 class PageRendererViewHelper extends AbstractViewHelper
40 {
41 use CompileWithRenderStatic;
42
43 /**
44 * Initialize arguments.
45 *
46 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
47 */
48 public function initializeArguments()
49 {
50 parent::initializeArguments();
51 $this->registerArgument('pageTitle', 'string', 'title tag of the module. Not required by default, as BE modules are shown in a frame', false, '');
52 $this->registerArgument('includeCssFiles', 'array', 'List of custom CSS file to be loaded');
53 $this->registerArgument('includeJsFiles', 'array', 'List of custom JavaScript file to be loaded');
54 $this->registerArgument('addJsInlineLabels', 'array', 'Custom labels to add to JavaScript inline labels');
55 $this->registerArgument('includeRequireJsModules', 'array', 'List of RequireJS modules to be loaded');
56 $this->registerArgument('addInlineSettings', 'array', 'Adds Javascript Inline Setting');
57 }
58
59 /**
60 * @param array $arguments
61 * @param \Closure $renderChildrenClosure
62 * @param RenderingContextInterface $renderingContext
63 */
64 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
65 {
66 $pageRenderer = static::getPageRenderer();
67 $pageTitle = $arguments['pageTitle'];
68 $includeCssFiles = $arguments['includeCssFiles'];
69 $includeJsFiles = $arguments['includeJsFiles'];
70 $addJsInlineLabels = $arguments['addJsInlineLabels'];
71 $includeRequireJsModules = $arguments['includeRequireJsModules'];
72 $addInlineSettings = $arguments['addInlineSettings'];
73
74 if ($pageTitle) {
75 $pageRenderer->setTitle($pageTitle);
76 }
77
78 // Include custom CSS and JS files
79 if (is_array($includeCssFiles) && count($includeCssFiles) > 0) {
80 foreach ($includeCssFiles as $addCssFile) {
81 $pageRenderer->addCssFile($addCssFile);
82 }
83 }
84 if (is_array($includeJsFiles) && count($includeJsFiles) > 0) {
85 foreach ($includeJsFiles as $addJsFile) {
86 $pageRenderer->addJsFile($addJsFile);
87 }
88 }
89 if (is_array($includeRequireJsModules) && count($includeRequireJsModules) > 0) {
90 foreach ($includeRequireJsModules as $addRequireJsFile) {
91 $pageRenderer->loadRequireJsModule($addRequireJsFile);
92 }
93 }
94
95 if (is_array($addInlineSettings) && count($addInlineSettings) > 0) {
96 $pageRenderer->addInlineSettingArray(null, $addInlineSettings);
97 }
98
99 // Add inline language labels
100 if (is_array($addJsInlineLabels) && count($addJsInlineLabels) > 0) {
101 $extensionKey = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionKey();
102 foreach ($addJsInlineLabels as $key) {
103 $label = LocalizationUtility::translate($key, $extensionKey);
104 $pageRenderer->addInlineLanguageLabel($key, $label);
105 }
106 }
107 }
108
109 /**
110 * @return PageRenderer
111 */
112 protected static function getPageRenderer()
113 {
114 return GeneralUtility::makeInstance(PageRenderer::class);
115 }
116 }