TYPO3  7.6
AbstractWidgetViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Widget;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
27 {
36  protected $controller;
37 
44  protected $ajaxWidget = false;
45 
50 
54  protected $objectManager;
55 
60  protected $extensionService;
61 
65  private $widgetContext;
66 
72  {
73  $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
74  }
75 
80  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
81  {
82  $this->objectManager = $objectManager;
83  $this->widgetContext = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
84  }
85 
91  public function initializeArgumentsAndRender()
92  {
93  $this->validateArguments();
94  $this->initialize();
95  $this->initializeWidgetContext();
96  return $this->callRenderMethod();
97  }
98 
104  private function initializeWidgetContext()
105  {
106  $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
108  $this->widgetContext->setControllerObjectName(get_class($this->controller));
109  $extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
110  $pluginName = $this->controllerContext->getRequest()->getPluginName();
111  $this->widgetContext->setParentExtensionName($extensionName);
112  $this->widgetContext->setParentPluginName($pluginName);
113  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
114  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
115  $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
116  if ($this->ajaxWidget === true) {
117  $this->ajaxWidgetContextHolder->store($this->widgetContext);
118  }
119  }
120 
128  public function setChildNodes(array $childNodes)
129  {
130  $rootNode = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode::class);
131  foreach ($childNodes as $childNode) {
132  $rootNode->addChildNode($childNode);
133  }
134  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
135  }
136 
143  protected function getWidgetConfiguration()
144  {
145  return $this->arguments;
146  }
147 
155  protected function initiateSubRequest()
156  {
157  if (!$this->controller instanceof \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
158  if (isset($this->controller)) {
159  throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException('initiateSubRequest() can not be called if there is no valid controller extending TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController. Got "' . get_class($this->controller) . '" in class "' . get_class($this) . '".', 1289422564);
160  }
161  throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add a corresponding injectController method to your WidgetViewHelper class "' . get_class($this) . '".', 1284401632);
162  }
163  $subRequest = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
164  $subRequest->setWidgetContext($this->widgetContext);
165  $this->passArgumentsToSubRequest($subRequest);
166  $subResponse = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
167  $this->controller->processRequest($subRequest, $subResponse);
168  return $subResponse;
169  }
170 
177  private function passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
178  {
179  $arguments = $this->controllerContext->getRequest()->getArguments();
180  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
181  if (isset($arguments[$widgetIdentifier])) {
182  if (isset($arguments[$widgetIdentifier]['action'])) {
183  $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
184  unset($arguments[$widgetIdentifier]['action']);
185  }
186  $subRequest->setArguments($arguments[$widgetIdentifier]);
187  }
188  }
189 
198  private function initializeWidgetIdentifier()
199  {
200  if (!$this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber')) {
201  $widgetCounter = 0;
202  } else {
203  $widgetCounter = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber');
204  }
205  $widgetIdentifier = '@widget_' . $widgetCounter;
206  $this->viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', $widgetCounter + 1);
207  $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
208  }
209 }