[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Widget / AbstractWidgetViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Widget;
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\Compiler\TemplateCompiler;
18 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20
21 abstract class AbstractWidgetViewHelper extends AbstractViewHelper
22 {
23 /**
24 * The Controller associated to this widget.
25 * This needs to be filled by the individual subclass by an inject method.
26 *
27 * @var \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
28 */
29 protected $controller;
30
31 /**
32 * If set to TRUE, it is an AJAX widget.
33 *
34 * @var bool
35 */
36 protected $ajaxWidget = false;
37
38 /**
39 * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
40 */
41 private $ajaxWidgetContextHolder;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
45 */
46 protected $objectManager;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
50 */
51 protected $extensionService;
52
53 /**
54 * @var bool
55 */
56 protected $escapeOutput = false;
57
58 /**
59 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
60 */
61 private $widgetContext;
62
63 /**
64 * @param \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder
65 * @internal
66 */
67 public function injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
68 {
69 $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
70 }
71
72 /**
73 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
74 * @internal
75 */
76 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
77 {
78 $this->objectManager = $objectManager;
79 $this->widgetContext = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
80 }
81
82 /**
83 * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService
84 * @internal
85 */
86 public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
87 {
88 $this->extensionService = $extensionService;
89 }
90
91 /**
92 * Initialize arguments.
93 * @internal
94 */
95 public function initializeArguments()
96 {
97 $this->registerArgument(
98 'customWidgetId',
99 'string',
100 'extend the widget identifier with a custom widget id',
101 false,
102 null
103 );
104 }
105
106 /**
107 * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
108 *
109 * @return string the rendered ViewHelper.
110 * @internal
111 */
112 public function initializeArgumentsAndRender()
113 {
114 $this->validateArguments();
115 $this->initialize();
116 $this->initializeWidgetContext();
117 return $this->callRenderMethod();
118 }
119
120 /**
121 * Initialize the Widget Context, before the Render method is called.
122 */
123 private function initializeWidgetContext()
124 {
125 $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
126 $this->initializeWidgetIdentifier();
127 $this->widgetContext->setControllerObjectName(get_class($this->controller));
128 $extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
129 $pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
130 $this->widgetContext->setParentExtensionName($extensionName);
131 $this->widgetContext->setParentPluginName($pluginName);
132 $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
133 $this->widgetContext->setParentPluginNamespace($pluginNamespace);
134 $this->widgetContext->setWidgetViewHelperClassName(static::class);
135 if ($this->ajaxWidget === true) {
136 $this->ajaxWidgetContextHolder->store($this->widgetContext);
137 }
138 }
139
140 /**
141 * Stores the syntax tree child nodes in the Widget Context, so they can be
142 * rendered with <f:widget.renderChildren> later on.
143 *
144 * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
145 * @internal
146 */
147 public function setChildNodes(array $childNodes)
148 {
149 $rootNode = $this->objectManager->get(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class);
150 foreach ($childNodes as $childNode) {
151 $rootNode->addChildNode($childNode);
152 }
153 $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
154 }
155
156 /**
157 * Generate the configuration for this widget. Override to adjust.
158 *
159 * @return array
160 */
161 protected function getWidgetConfiguration()
162 {
163 return $this->arguments;
164 }
165
166 /**
167 * Initiate a sub request to $this->controller. Make sure to fill $this->controller
168 * via Dependency Injection.
169 *
170 * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface the response of this request.
171 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException
172 */
173 protected function initiateSubRequest()
174 {
175 if (!isset($this->controller) || !$this->controller instanceof \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
176 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException(
177 'initiateSubRequest() can not be called if there is no valid controller extending ' .
178 'TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController' .
179 ' Got "' . ($this->controller ? get_class($this->controller) : gettype($this->controller)) .
180 '" in class "' . static::class . '".',
181 1289422564
182 );
183 }
184 $subRequest = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
185 $subRequest->setWidgetContext($this->widgetContext);
186 $this->passArgumentsToSubRequest($subRequest);
187 $subResponse = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
188 $this->controller->processRequest($subRequest, $subResponse);
189 return $subResponse;
190 }
191
192 /**
193 * Pass the arguments of the widget to the subrequest.
194 *
195 * @param \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest
196 */
197 private function passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
198 {
199 $arguments = $this->renderingContext->getControllerContext()->getRequest()->getArguments();
200 $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
201 if (isset($arguments[$widgetIdentifier])) {
202 if (isset($arguments[$widgetIdentifier]['action'])) {
203 $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
204 unset($arguments[$widgetIdentifier]['action']);
205 }
206 $subRequest->setArguments($arguments[$widgetIdentifier]);
207 }
208 }
209
210 /**
211 * The widget identifier is unique on the current page, and is used
212 * in the URI as a namespace for the widget's arguments.
213 *
214 * @return string the widget identifier for this widget
215 * @todo clean up, and make it somehow more routing compatible.
216 */
217 private function initializeWidgetIdentifier()
218 {
219 $widgetCounter = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', 0);
220 $widgetIdentifier = '@widget_' . ((isset($this->arguments['customWidgetId']) && $this->arguments['customWidgetId'] !== null) ? $this->arguments['customWidgetId'] . '_' : '') . $widgetCounter;
221 $this->viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', $widgetCounter + 1);
222 $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
223 }
224
225 /**
226 * @param string $argumentsName
227 * @param string $closureName
228 * @param string $initializationPhpCode
229 * @param ViewHelperNode $node
230 * @param TemplateCompiler $compiler
231 * @return string
232 */
233 public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
234 {
235 $compiler->disable();
236 return '\'\'';
237 }
238 }