[TASK] Remove usages of getTemplateVariableContainer
[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 * @param array $arguments
37 * @return string
38 */
39 public function render(array $arguments = [])
40 {
41 $renderingContext = $this->getWidgetRenderingContext();
42 $widgetChildNodes = $this->getWidgetChildNodes();
43 $this->addArgumentsToTemplateVariableContainer($arguments);
44 $output = $widgetChildNodes->evaluate($renderingContext);
45 $this->removeArgumentsFromTemplateVariableContainer($arguments);
46 return $output;
47 }
48
49 /**
50 * Get the widget rendering context, or throw an exception if it cannot be found.
51 *
52 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
53 * @return \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
54 */
55 protected function getWidgetRenderingContext()
56 {
57 $renderingContext = $this->getWidgetContext()->getViewHelperChildNodeRenderingContext();
58 if (!$renderingContext instanceof \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface) {
59 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);
60 }
61 return $renderingContext;
62 }
63
64 /**
65 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
66 */
67 protected function getWidgetChildNodes()
68 {
69 return $this->getWidgetContext()->getViewHelperChildNodes();
70 }
71
72 /**
73 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
74 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
75 */
76 protected function getWidgetContext()
77 {
78 $request = $this->controllerContext->getRequest();
79 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
80 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);
81 }
82 return $request->getWidgetContext();
83 }
84
85 /**
86 * Add the given arguments to the TemplateVariableContainer of the widget.
87 *
88 * @param array $arguments
89 * @return void
90 */
91 protected function addArgumentsToTemplateVariableContainer(array $arguments)
92 {
93 $templateVariableContainer = $this->getWidgetRenderingContext()->getVariableProvider();
94 foreach ($arguments as $identifier => $value) {
95 $templateVariableContainer->add($identifier, $value);
96 }
97 }
98
99 /**
100 * Remove the given arguments from the TemplateVariableContainer of the widget.
101 *
102 * @param array $arguments
103 * @return void
104 */
105 protected function removeArgumentsFromTemplateVariableContainer(array $arguments)
106 {
107 $templateVariableContainer = $this->getWidgetRenderingContext()->getVariableProvider();
108 foreach ($arguments as $identifier => $value) {
109 $templateVariableContainer->remove($identifier);
110 }
111 }
112 }