TYPO3 CMS  TYPO3_6-2
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 
36  protected $controller;
37 
44  protected $ajaxWidget = FALSE;
45 
50 
54  protected $objectManager;
55 
60  protected $extensionService;
61 
65  private $widgetContext;
66 
72  $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
73  }
74 
79  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
80  $this->objectManager = $objectManager;
81  $this->widgetContext = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetContext');
82  }
83 
89  public function initializeArgumentsAndRender() {
90  $this->validateArguments();
91  $this->initialize();
92  $this->initializeWidgetContext();
93  return $this->callRenderMethod();
94  }
95 
101  private function initializeWidgetContext() {
102  $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
104  $this->widgetContext->setControllerObjectName(get_class($this->controller));
105  $extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
106  $pluginName = $this->controllerContext->getRequest()->getPluginName();
107  $this->widgetContext->setParentExtensionName($extensionName);
108  $this->widgetContext->setParentPluginName($pluginName);
109  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
110  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
111  $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
112  if ($this->ajaxWidget === TRUE) {
113  $this->ajaxWidgetContextHolder->store($this->widgetContext);
114  }
115  }
116 
124  public function setChildNodes(array $childNodes) {
125  $rootNode = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\RootNode');
126  foreach ($childNodes as $childNode) {
127  $rootNode->addChildNode($childNode);
128  }
129  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
130  }
131 
138  protected function getWidgetConfiguration() {
139  return $this->arguments;
140  }
141 
149  protected function initiateSubRequest() {
150  if (!$this->controller instanceof \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
151  if (isset($this->controller)) {
152  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);
153  }
154  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);
155  }
156  $subRequest = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Widget\\WidgetRequest');
157  $subRequest->setWidgetContext($this->widgetContext);
158  $this->passArgumentsToSubRequest($subRequest);
159  $subResponse = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response');
160  $this->controller->processRequest($subRequest, $subResponse);
161  return $subResponse;
162  }
163 
170  private function passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest) {
171  $arguments = $this->controllerContext->getRequest()->getArguments();
172  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
173  if (isset($arguments[$widgetIdentifier])) {
174  if (isset($arguments[$widgetIdentifier]['action'])) {
175  $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
176  unset($arguments[$widgetIdentifier]['action']);
177  }
178  $subRequest->setArguments($arguments[$widgetIdentifier]);
179  }
180  }
181 
190  private function initializeWidgetIdentifier() {
191  if (!$this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetViewHelper', 'nextWidgetNumber')) {
192  $widgetCounter = 0;
193  } else {
194  $widgetCounter = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetViewHelper', 'nextWidgetNumber');
195  }
196  $widgetIdentifier = '@widget_' . $widgetCounter;
197  $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetViewHelper', 'nextWidgetNumber', $widgetCounter + 1);
198  $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
199  }
200 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)