‪TYPO3CMS  9.5
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 
17 use TYPO3Fluid\Fluid\Component\ComponentInterface;
18 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
19 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 
25 abstract class ‪AbstractWidgetViewHelper extends AbstractViewHelper
26 {
33  protected ‪$controller;
34 
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 
90  public function ‪injectExtensionService(\‪TYPO3\CMS\‪Extbase\Service\ExtensionService ‪$extensionService)
91  {
92  $this->extensionService = ‪$extensionService;
93  }
94 
99  public function ‪initializeArguments()
100  {
101  $this->registerArgument(
102  'customWidgetId',
103  'string',
104  'extend the widget identifier with a custom widget id',
105  false,
106  null
107  );
108  }
109 
116  public function ‪initializeArgumentsAndRender()
117  {
118  $this->validateArguments();
119  $this->initialize();
121  return $this->callRenderMethod();
122  }
123 
131  public function ‪evaluate(RenderingContextInterface $renderingContext)
132  {
133  $this->renderingContext = $renderingContext;
134  $this->getArguments()->setRenderingContext($renderingContext);
136  return $this->callRenderMethod();
137  }
138 
147  public function ‪onClose(RenderingContextInterface $renderingContext): ComponentInterface
148  {
149  $node = parent::onClose($renderingContext);
150  $rootNode = $this->objectManager->get(\‪TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class);
151  $rootNode->setChildren($this->getChildren());
152  $this->widgetContext->setViewHelperChildNodes($rootNode, $renderingContext);
153  return $node;
154  }
155 
159  private function ‪initializeWidgetContext()
160  {
161  $this->widgetContext->setWidgetConfiguration($this->‪getWidgetConfiguration());
163  $this->widgetContext->setControllerObjectName(get_class($this->controller));
164  $vendorName = $this->renderingContext->getControllerContext()->getRequest()->getControllerVendorName();
165  $extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
166  $pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
167  $this->widgetContext->setParentVendorName($vendorName);
168  $this->widgetContext->setParentExtensionName($extensionName);
169  $this->widgetContext->setParentPluginName($pluginName);
170  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
171  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
172  $this->widgetContext->setWidgetViewHelperClassName(static::class);
173  if ($this->ajaxWidget === true) {
174  $this->ajaxWidgetContextHolder->store($this->widgetContext);
175  }
176  }
177 
185  public function ‪setChildNodes(array $childNodes)
186  {
187  $rootNode = $this->objectManager->get(\‪TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode::class);
188  foreach ($childNodes as $childNode) {
189  $rootNode->addChildNode($childNode);
190  }
191  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
192  }
193 
199  protected function ‪getWidgetConfiguration()
200  {
201  return $this->arguments;
202  }
203 
211  protected function ‪initiateSubRequest()
212  {
213  if (!isset($this->controller) || !$this->controller instanceof \‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController) {
214  throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException(
215  'initiateSubRequest() can not be called if there is no valid controller extending ' .
216  'TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController' .
217  ' Got "' . ($this->controller ? get_class($this->controller) : gettype($this->controller)) .
218  '" in class "' . static::class . '".',
219  1289422564
220  );
221  }
222  $subRequest = $this->objectManager->get(\‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
223  $subRequest->setWidgetContext($this->widgetContext);
224  $this->‪passArgumentsToSubRequest($subRequest);
225  $subResponse = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Web\Response::class);
226  $this->controller->processRequest($subRequest, $subResponse);
227  return $subResponse;
228  }
229 
235  private function ‪passArgumentsToSubRequest(\‪TYPO3\CMS\Fluid\Core\Widget\‪WidgetRequest $subRequest)
236  {
237  $arguments = $this->renderingContext->getControllerContext()->getRequest()->getArguments();
238  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
239  if (isset($arguments[$widgetIdentifier])) {
240  if (isset($arguments[$widgetIdentifier]['action'])) {
241  $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
242  unset($arguments[$widgetIdentifier]['action']);
243  }
244  $subRequest->setArguments($arguments[$widgetIdentifier]);
245  }
246  }
247 
255  private function ‪initializeWidgetIdentifier()
256  {
257  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
258  $widgetCounter = $viewHelperVariableContainer->get(\‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', 0);
259  $widgetIdentifier = '@widget_' . ((isset($this->arguments['customWidgetId']) && $this->arguments['customWidgetId'] !== null) ? $this->arguments['customWidgetId'] . '_' : '') . $widgetCounter;
260  $viewHelperVariableContainer->addOrUpdate(\‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', $widgetCounter + 1);
261  $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
262  }
263 
272  public function ‪compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
273  {
274  $compiler->disable();
275  return '\'\'';
276  }
277 }
‪TYPO3\CMS\Fluid\Core\Widget
Definition: AbstractWidgetController.php:2
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$ajaxWidgetContextHolder
‪TYPO3 CMS Fluid Core Widget AjaxWidgetContextHolder $ajaxWidgetContextHolder
Definition: AbstractWidgetViewHelper.php:42
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$extensionService
‪TYPO3 CMS Extbase Service ExtensionService $extensionService
Definition: AbstractWidgetViewHelper.php:50
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
Definition: WidgetRequest.php:22
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: AbstractWidgetViewHelper.php:73
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectExtensionService
‪injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
Definition: AbstractWidgetViewHelper.php:83
‪TYPO3Fluid
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\evaluate
‪string evaluate(RenderingContextInterface $renderingContext)
Definition: AbstractWidgetViewHelper.php:124
‪TYPO3
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\getWidgetConfiguration
‪array getWidgetConfiguration()
Definition: AbstractWidgetViewHelper.php:192
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeArgumentsAndRender
‪string initializeArgumentsAndRender()
Definition: AbstractWidgetViewHelper.php:109
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: AbstractWidgetViewHelper.php:54
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper
Definition: AbstractWidgetViewHelper.php:26
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$ajaxWidget
‪bool $ajaxWidget
Definition: AbstractWidgetViewHelper.php:38
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$widgetContext
‪TYPO3 CMS Fluid Core Widget WidgetContext $widgetContext
Definition: AbstractWidgetViewHelper.php:58
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$controller
‪TYPO3 CMS Fluid Core Widget AbstractWidgetController $controller
Definition: AbstractWidgetViewHelper.php:32
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectAjaxWidgetContextHolder
‪injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
Definition: AbstractWidgetViewHelper.php:64
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: AbstractWidgetViewHelper.php:46
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractWidgetViewHelper.php:92
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\passArgumentsToSubRequest
‪passArgumentsToSubRequest(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest $subRequest)
Definition: AbstractWidgetViewHelper.php:228
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\onClose
‪ComponentInterface onClose(RenderingContextInterface $renderingContext)
Definition: AbstractWidgetViewHelper.php:140
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\compile
‪string compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
Definition: AbstractWidgetViewHelper.php:265
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:29
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\setChildNodes
‪setChildNodes(array $childNodes)
Definition: AbstractWidgetViewHelper.php:178
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeWidgetContext
‪initializeWidgetContext()
Definition: AbstractWidgetViewHelper.php:152
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:25
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeWidgetIdentifier
‪string initializeWidgetIdentifier()
Definition: AbstractWidgetViewHelper.php:248
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initiateSubRequest
‪TYPO3 CMS Extbase Mvc ResponseInterface initiateSubRequest()
Definition: AbstractWidgetViewHelper.php:204