TYPO3 CMS  TYPO3_7-6
AbstractWidgetViewHelper.php
Go to the documentation of this file.
1 <?php
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 {
35  protected $controller;
36 
43  protected $ajaxWidget = false;
44 
49 
53  protected $objectManager;
54 
58  protected $extensionService;
59 
63  private $widgetContext;
64 
70  {
71  $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
72  }
73 
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 
87  public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
88  {
89  $this->extensionService = $extensionService;
90  }
91 
97  public function initializeArgumentsAndRender()
98  {
99  $this->validateArguments();
100  $this->initialize();
101  $this->initializeWidgetContext();
102  return $this->callRenderMethod();
103  }
104 
110  private function initializeWidgetContext()
111  {
112  $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
114  $this->widgetContext->setControllerObjectName(get_class($this->controller));
115  $extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
116  $pluginName = $this->controllerContext->getRequest()->getPluginName();
117  $this->widgetContext->setParentExtensionName($extensionName);
118  $this->widgetContext->setParentPluginName($pluginName);
119  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
120  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
121  $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
122  if ($this->ajaxWidget === true) {
123  $this->ajaxWidgetContextHolder->store($this->widgetContext);
124  }
125  }
126 
134  public function setChildNodes(array $childNodes)
135  {
136  $rootNode = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode::class);
137  foreach ($childNodes as $childNode) {
138  $rootNode->addChildNode($childNode);
139  }
140  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
141  }
142 
149  protected function getWidgetConfiguration()
150  {
151  return $this->arguments;
152  }
153 
161  protected function initiateSubRequest()
162  {
163  if (!$this->controller instanceof \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
164  if (isset($this->controller)) {
165  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);
166  }
167  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);
168  }
169  $subRequest = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
170  $subRequest->setWidgetContext($this->widgetContext);
171  $this->passArgumentsToSubRequest($subRequest);
172  $subResponse = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
173  $this->controller->processRequest($subRequest, $subResponse);
174  return $subResponse;
175  }
176 
183  private function passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
184  {
185  $arguments = $this->controllerContext->getRequest()->getArguments();
186  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
187  if (isset($arguments[$widgetIdentifier])) {
188  if (isset($arguments[$widgetIdentifier]['action'])) {
189  $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
190  unset($arguments[$widgetIdentifier]['action']);
191  }
192  $subRequest->setArguments($arguments[$widgetIdentifier]);
193  }
194  }
195 
204  private function initializeWidgetIdentifier()
205  {
206  if (!$this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber')) {
207  $widgetCounter = 0;
208  } else {
209  $widgetCounter = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber');
210  }
211  $widgetIdentifier = '@widget_' . $widgetCounter;
212  $this->viewHelperVariableContainer->addOrUpdate(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', $widgetCounter + 1);
213  $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
214  }
215 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)