[CLEANUP] Rework/simplify copyright header and remove @package
[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 backported from the TYPO3 Flow package "TYPO3.Fluid".
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
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 /**
31 * @param array $arguments
32 * @return string
33 */
34 public function render(array $arguments = array()) {
35 $renderingContext = $this->getWidgetRenderingContext();
36 $widgetChildNodes = $this->getWidgetChildNodes();
37 $this->addArgumentsToTemplateVariableContainer($arguments);
38 $output = $widgetChildNodes->evaluate($renderingContext);
39 $this->removeArgumentsFromTemplateVariableContainer($arguments);
40 return $output;
41 }
42
43 /**
44 * Get the widget rendering context, or throw an exception if it cannot be found.
45 *
46 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
47 * @return \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface
48 */
49 protected function getWidgetRenderingContext() {
50 $renderingContext = $this->getWidgetContext()->getViewHelperChildNodeRenderingContext();
51 if (!$renderingContext instanceof \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface) {
52 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);
53 }
54 return $renderingContext;
55 }
56
57 /**
58 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
59 */
60 protected function getWidgetChildNodes() {
61 return $this->getWidgetContext()->getViewHelperChildNodes();
62 }
63
64 /**
65 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
66 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
67 */
68 protected function getWidgetContext() {
69 $request = $this->controllerContext->getRequest();
70 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
71 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);
72 }
73 return $request->getWidgetContext();
74 }
75
76 /**
77 * Add the given arguments to the TemplateVariableContainer of the widget.
78 *
79 * @param array $arguments
80 * @return void
81 */
82 protected function addArgumentsToTemplateVariableContainer(array $arguments) {
83 $templateVariableContainer = $this->getWidgetRenderingContext()->getTemplateVariableContainer();
84 foreach ($arguments as $identifier => $value) {
85 $templateVariableContainer->add($identifier, $value);
86 }
87 }
88
89 /**
90 * Remove the given arguments from the TemplateVariableContainer of the widget.
91 *
92 * @param array $arguments
93 * @return void
94 */
95 protected function removeArgumentsFromTemplateVariableContainer(array $arguments) {
96 $templateVariableContainer = $this->getWidgetRenderingContext()->getTemplateVariableContainer();
97 foreach ($arguments as $identifier => $value) {
98 $templateVariableContainer->remove($identifier);
99 }
100 }
101 }