[TASK] Convert remaining ViewHelpers to static calling
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / RenderChildrenViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
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 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
17 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
18
19 /**
20 * Render the inner parts of a Widget.
21 * This ViewHelper can only be used in a template which belongs to a Widget Controller.
22 *
23 * It renders everything inside the Widget ViewHelper, and you can pass additional
24 * arguments.
25 *
26 * @api
27 */
28 class RenderChildrenViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
29 {
30 use CompileWithRenderStatic;
31
32 /**
33 * As this ViewHelper might render HTML, the output must not be escaped
34 *
35 * @var bool
36 */
37 protected $escapeOutput = false;
38
39 /**
40 * Initialize arguments
41 */
42 public function initializeArguments()
43 {
44 parent::initializeArguments();
45 $this->registerArgument('arguments', 'array', 'Arguments to assign as template variables', false, []);
46 }
47
48 /**
49 * @param array $arguments
50 * @param \Closure $renderChildrenClosure
51 * @param RenderingContextInterface $renderingContext
52 * @return mixed
53 */
54 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
55 {
56 $subRenderingContext = static::getWidgetRenderingContext($renderingContext);
57 $widgetChildNodes = static::getWidgetChildNodes($renderingContext);
58 static::addArgumentsToTemplateVariableContainer($subRenderingContext, $arguments['arguments']);
59 $output = $widgetChildNodes->evaluate($subRenderingContext);
60 static::removeArgumentsFromTemplateVariableContainer($subRenderingContext, $arguments['arguments']);
61 return $output;
62 }
63
64 /**
65 * Get the widget rendering context, or throw an exception if it cannot be found.
66 *
67 * @param RenderingContextInterface $renderingContext
68 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
69 * @return \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
70 */
71 protected static function getWidgetRenderingContext(RenderingContextInterface $renderingContext)
72 {
73 $subRenderingContext = static::getWidgetContext($renderingContext)->getViewHelperChildNodeRenderingContext();
74 if (!$subRenderingContext instanceof \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface) {
75 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException('Rendering Context not found inside Widget. <f:renderChildren> has been used in an AJAX Request, but is only usable in non-ajax mode.', 1284986604);
76 }
77 return $subRenderingContext;
78 }
79
80 /**
81 * @param RenderingContextInterface $renderingContext
82 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
83 */
84 protected static function getWidgetChildNodes(RenderingContextInterface $renderingContext)
85 {
86 return static::getWidgetContext($renderingContext)->getViewHelperChildNodes();
87 }
88
89 /**
90 * @param RenderingContextInterface $renderingContext
91 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
92 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
93 */
94 protected static function getWidgetContext(RenderingContextInterface $renderingContext)
95 {
96 $request = $renderingContext->getControllerContext()->getRequest();
97 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
98 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException('The Request is not a WidgetRequest! <f:renderChildren> must be called inside a Widget Template.', 1284986120);
99 }
100 return $request->getWidgetContext();
101 }
102
103 /**
104 * Add the given arguments to the TemplateVariableContainer of the widget.
105 *
106 * @param RenderingContextInterface $renderingContext
107 * @param array $arguments
108 */
109 protected static function addArgumentsToTemplateVariableContainer(RenderingContextInterface $renderingContext, array $arguments)
110 {
111 $templateVariableContainer = $renderingContext->getVariableProvider();
112 foreach ($arguments as $identifier => $value) {
113 $templateVariableContainer->add($identifier, $value);
114 }
115 }
116
117 /**
118 * Remove the given arguments from the TemplateVariableContainer of the widget.
119 *
120 * @param RenderingContextInterface $renderingContext
121 * @param array $arguments
122 */
123 protected static function removeArgumentsFromTemplateVariableContainer(RenderingContextInterface $renderingContext, array $arguments)
124 {
125 $templateVariableContainer = $renderingContext->getVariableProvider();
126 foreach ($arguments as $identifier => $value) {
127 $templateVariableContainer->remove($identifier);
128 }
129 }
130 }