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