61451c138e70ffc817dedae4d7f5217ba57600f2
[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 /**
18 * Render the inner parts of a Widget.
19 * This ViewHelper can only be used in a template which belongs to a Widget Controller.
20 *
21 * It renders everything inside the Widget ViewHelper, and you can pass additional
22 * arguments.
23 *
24 * @api
25 */
26 class RenderChildrenViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
27 {
28 /**
29 * As this ViewHelper might render HTML, the output must not be escaped
30 *
31 * @var bool
32 */
33 protected $escapeOutput = false;
34
35 /**
36 * Initialize arguments
37 */
38 public function initializeArguments()
39 {
40 parent::initializeArguments();
41 $this->registerArgument('arguments', 'array', 'Arguments to assign as template variables', false, []);
42 }
43
44 /**
45 * @return string
46 */
47 public function render()
48 {
49 $renderingContext = $this->getWidgetRenderingContext();
50 $widgetChildNodes = $this->getWidgetChildNodes();
51 $this->addArgumentsToTemplateVariableContainer($this->arguments['arguments']);
52 $output = $widgetChildNodes->evaluate($renderingContext);
53 $this->removeArgumentsFromTemplateVariableContainer($this->arguments['arguments']);
54 return $output;
55 }
56
57 /**
58 * Get the widget rendering context, or throw an exception if it cannot be found.
59 *
60 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
61 * @return \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
62 */
63 protected function getWidgetRenderingContext()
64 {
65 $renderingContext = $this->getWidgetContext()->getViewHelperChildNodeRenderingContext();
66 if (!$renderingContext instanceof \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface) {
67 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);
68 }
69 return $renderingContext;
70 }
71
72 /**
73 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
74 */
75 protected function getWidgetChildNodes()
76 {
77 return $this->getWidgetContext()->getViewHelperChildNodes();
78 }
79
80 /**
81 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
82 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
83 */
84 protected function getWidgetContext()
85 {
86 $request = $this->controllerContext->getRequest();
87 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
88 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);
89 }
90 return $request->getWidgetContext();
91 }
92
93 /**
94 * Add the given arguments to the TemplateVariableContainer of the widget.
95 *
96 * @param array $arguments
97 */
98 protected function addArgumentsToTemplateVariableContainer(array $arguments)
99 {
100 $templateVariableContainer = $this->getWidgetRenderingContext()->getVariableProvider();
101 foreach ($arguments as $identifier => $value) {
102 $templateVariableContainer->add($identifier, $value);
103 }
104 }
105
106 /**
107 * Remove the given arguments from the TemplateVariableContainer of the widget.
108 *
109 * @param array $arguments
110 */
111 protected function removeArgumentsFromTemplateVariableContainer(array $arguments)
112 {
113 $templateVariableContainer = $this->getWidgetRenderingContext()->getVariableProvider();
114 foreach ($arguments as $identifier => $value) {
115 $templateVariableContainer->remove($identifier);
116 }
117 }
118 }