TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractWidgetViewHelper.php
Go to the documentation of this file.
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 
24 {
32  protected $controller;
33 
40  protected $ajaxWidget = false;
41 
46 
50  protected $objectManager;
51 
55  protected $extensionService;
56 
60  protected $escapeOutput = false;
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 
89  public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
90  {
91  $this->extensionService = $extensionService;
92  }
93 
99  public function initializeArgumentsAndRender()
100  {
101  $this->validateArguments();
102  $this->initialize();
103  $this->initializeWidgetContext();
104  return $this->callRenderMethod();
105  }
106 
112  private function initializeWidgetContext()
113  {
114  $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
116  $this->widgetContext->setControllerObjectName(get_class($this->controller));
117  $extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
118  $pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
119  $this->widgetContext->setParentExtensionName($extensionName);
120  $this->widgetContext->setParentPluginName($pluginName);
121  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
122  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
123  $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
124  if ($this->ajaxWidget === true) {
125  $this->ajaxWidgetContextHolder->store($this->widgetContext);
126  }
127  }
128 
136  public function setChildNodes(array $childNodes)
137  {
138  $rootNode = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode::class);
139  foreach ($childNodes as $childNode) {
140  $rootNode->addChildNode($childNode);
141  }
142  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
143  }
144 
151  protected function getWidgetConfiguration()
152  {
153  return $this->arguments;
154  }
155 
164  protected function initiateSubRequest()
165  {
166  if (!isset($this->controller) || !$this->controller instanceof \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
167  throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException(
168  'initiateSubRequest() can not be called if there is no valid controller extending ' .
169  'TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController' .
170  ' Got "' . ($this->controller ? get_class($this->controller) : gettype($this->controller)) .
171  '" in class "' . get_class($this) . '".', 1289422564);
172  }
173  $subRequest = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
174  $subRequest->setWidgetContext($this->widgetContext);
175  $this->passArgumentsToSubRequest($subRequest);
176  $subResponse = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Response::class);
177  $this->controller->processRequest($subRequest, $subResponse);
178  return $subResponse;
179  }
180 
187  private function passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
188  {
189  $arguments = $this->renderingContext->getControllerContext()->getRequest()->getArguments();
190  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
191  if (isset($arguments[$widgetIdentifier])) {
192  if (isset($arguments[$widgetIdentifier]['action'])) {
193  $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
194  unset($arguments[$widgetIdentifier]['action']);
195  }
196  $subRequest->setArguments($arguments[$widgetIdentifier]);
197  }
198  }
199 
208  private function initializeWidgetIdentifier()
209  {
210  $widgetCounter = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', 0);
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 
223  public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
224  {
225  $compiler->disable();
226  return '\'\'';
227  }
228 }
passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)