[TASK] Streamline phpdoc annotations in EXT:fluid
[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
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
20
21 /**
22 * Render the inner parts of a Widget.
23 * This ViewHelper can only be used in a template which belongs to a Widget Controller.
24 *
25 * It renders everything inside the Widget ViewHelper, and you can pass additional
26 * arguments.
27 */
28 class RenderChildrenViewHelper extends 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 $this->registerArgument('arguments', 'array', 'Arguments to assign as template variables', false, []);
45 }
46
47 /**
48 * @param array $arguments
49 * @param \Closure $renderChildrenClosure
50 * @param RenderingContextInterface $renderingContext
51 * @return mixed
52 */
53 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
54 {
55 $subRenderingContext = static::getWidgetRenderingContext($renderingContext);
56 $widgetChildNodes = static::getWidgetChildNodes($renderingContext);
57 static::addArgumentsToTemplateVariableContainer($subRenderingContext, $arguments['arguments']);
58 $output = $widgetChildNodes->evaluate($subRenderingContext);
59 static::removeArgumentsFromTemplateVariableContainer($subRenderingContext, $arguments['arguments']);
60 return $output;
61 }
62
63 /**
64 * Get the widget rendering context, or throw an exception if it cannot be found.
65 *
66 * @param RenderingContextInterface $renderingContext
67 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
68 * @return \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
69 */
70 protected static function getWidgetRenderingContext(RenderingContextInterface $renderingContext)
71 {
72 $subRenderingContext = static::getWidgetContext($renderingContext)->getViewHelperChildNodeRenderingContext();
73 if (!$subRenderingContext instanceof \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface) {
74 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);
75 }
76 return $subRenderingContext;
77 }
78
79 /**
80 * @param RenderingContextInterface $renderingContext
81 * @return \TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode
82 */
83 protected static function getWidgetChildNodes(RenderingContextInterface $renderingContext)
84 {
85 return static::getWidgetContext($renderingContext)->getViewHelperChildNodes();
86 }
87
88 /**
89 * @param RenderingContextInterface $renderingContext
90 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
91 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
92 */
93 protected static function getWidgetContext(RenderingContextInterface $renderingContext)
94 {
95 $request = $renderingContext->getControllerContext()->getRequest();
96 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
97 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);
98 }
99 return $request->getWidgetContext();
100 }
101
102 /**
103 * Add the given arguments to the TemplateVariableContainer of the widget.
104 *
105 * @param RenderingContextInterface $renderingContext
106 * @param array $arguments
107 */
108 protected static function addArgumentsToTemplateVariableContainer(RenderingContextInterface $renderingContext, array $arguments)
109 {
110 $templateVariableContainer = $renderingContext->getVariableProvider();
111 foreach ($arguments as $identifier => $value) {
112 $templateVariableContainer->add($identifier, $value);
113 }
114 }
115
116 /**
117 * Remove the given arguments from the TemplateVariableContainer of the widget.
118 *
119 * @param RenderingContextInterface $renderingContext
120 * @param array $arguments
121 */
122 protected static function removeArgumentsFromTemplateVariableContainer(RenderingContextInterface $renderingContext, array $arguments)
123 {
124 $templateVariableContainer = $renderingContext->getVariableProvider();
125 foreach ($arguments as $identifier => $value) {
126 $templateVariableContainer->remove($identifier);
127 }
128 }
129 }