TYPO3 CMS  TYPO3_8-7
AbstractWidgetViewHelper.php
Go to the documentation of this file.
1 <?php
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 
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 
71  {
72  $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
73  }
74 
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 
95  public function initializeArguments()
96  {
97  $this->registerArgument(
98  'customWidgetId',
99  'string',
100  'extend the widget identifier with a custom widget id',
101  false,
102  null
103  );
104  }
105 
112  {
113  $this->validateArguments();
114  $this->initialize();
115  $this->initializeWidgetContext();
116  return $this->callRenderMethod();
117  }
118 
122  private function initializeWidgetContext()
123  {
124  $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
126  $this->widgetContext->setControllerObjectName(get_class($this->controller));
127  $vendorName = $this->renderingContext->getControllerContext()->getRequest()->getControllerVendorName();
128  $extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
129  $pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
130  $this->widgetContext->setParentVendorName($vendorName);
131  $this->widgetContext->setParentExtensionName($extensionName);
132  $this->widgetContext->setParentPluginName($pluginName);
133  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
134  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
135  $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
136  if ($this->ajaxWidget === true) {
137  $this->ajaxWidgetContextHolder->store($this->widgetContext);
138  }
139  }
140 
147  public function setChildNodes(array $childNodes)
148  {
149  $rootNode = $this->objectManager->get(\TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class);
150  foreach ($childNodes as $childNode) {
151  $rootNode->addChildNode($childNode);
152  }
153  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
154  }
155 
162  protected function getWidgetConfiguration()
163  {
164  return $this->arguments;
165  }
166 
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 "' . get_class($this) . '".',
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 
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 
219  private function initializeWidgetIdentifier()
220  {
221  $widgetCounter = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', 0);
222  $widgetIdentifier = '@widget_' . ($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 
234  public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
235  {
236  $compiler->disable();
237  return '\'\'';
238  }
239 }
compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
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)