‪TYPO3CMS  10.4
AbstractWidgetViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3Fluid\Fluid\Component\ComponentInterface;
23 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
24 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
25 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 
29 abstract class ‪AbstractWidgetViewHelper extends AbstractViewHelper
30 {
37  protected ‪$controller;
38 
44  protected ‪$ajaxWidget = false;
45 
50 
54  protected ‪$objectManager;
55 
59  protected ‪$extensionService;
60 
64  protected ‪$escapeOutput = false;
65 
69  private ‪$widgetContext;
70 
76  {
77  $this->ajaxWidgetContextHolder = ‪$ajaxWidgetContextHolder;
78  }
79 
85  {
86  $this->objectManager = ‪$objectManager;
87  $this->widgetContext = $this->objectManager->‪get(WidgetContext::class);
88  }
89 
95  {
96  $this->extensionService = ‪$extensionService;
97  }
98 
103  public function ‪initializeArguments()
104  {
105  $this->registerArgument(
106  'customWidgetId',
107  'string',
108  'extend the widget identifier with a custom widget id',
109  false,
110  null
111  );
112  $this->registerArgument(
113  'storeSession',
114  'bool',
115  'Store the widgets session (utilizing a cookie).',
116  false,
117  true
118  );
119  }
120 
127  public function ‪initializeArgumentsAndRender()
128  {
129  $this->validateArguments();
130  $this->initialize();
132  return $this->callRenderMethod();
133  }
134 
142  public function ‪evaluate(RenderingContextInterface $renderingContext)
143  {
144  $this->renderingContext = $renderingContext;
145  $this->getArguments()->setRenderingContext($renderingContext);
147  return $this->callRenderMethod();
148  }
149 
158  public function ‪onClose(RenderingContextInterface $renderingContext): ComponentInterface
159  {
160  $node = parent::onClose($renderingContext);
161  $rootNode = $this->objectManager->get(RootNode::class);
162  $rootNode->setChildren($this->getChildren());
163  $this->widgetContext->setViewHelperChildNodes($rootNode, $renderingContext);
164  return $node;
165  }
166 
170  private function ‪initializeWidgetContext()
171  {
172  $this->widgetContext->setWidgetConfiguration($this->‪getWidgetConfiguration());
174  $this->widgetContext->setControllerObjectName($this->controller === null ? 'null' : get_class($this->controller));
175  $extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
176  $pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
177  $this->widgetContext->setParentExtensionName($extensionName);
178  $this->widgetContext->setParentPluginName($pluginName);
179  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
180  $this->widgetContext->setParentPluginNamespace($pluginNamespace);
181  $this->widgetContext->setWidgetViewHelperClassName(static::class);
182  if ($this->ajaxWidget === true && $this->arguments['storeSession']) {
183  $this->ajaxWidgetContextHolder->store($this->widgetContext);
184  }
185  }
186 
194  public function ‪setChildNodes(array $childNodes)
195  {
196  $rootNode = $this->objectManager->get(RootNode::class);
197  foreach ($childNodes as $childNode) {
198  $rootNode->addChildNode($childNode);
199  }
200  $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
201  }
202 
208  protected function ‪getWidgetConfiguration()
209  {
210  return $this->arguments;
211  }
212 
220  protected function ‪initiateSubRequest()
221  {
222  if (!isset($this->controller) || !$this->controller instanceof AbstractWidgetController) {
223  throw new MissingControllerException(
224  'initiateSubRequest() can not be called if there is no valid controller extending ' .
225  AbstractWidgetController::class .
226  ' Got "' . ($this->controller ? get_class($this->controller) : gettype($this->controller)) .
227  '" in class "' . static::class . '".',
228  1289422564
229  );
230  }
231  $subRequest = $this->objectManager->get(WidgetRequest::class);
232  $subRequest->setWidgetContext($this->widgetContext);
233  $this->‪passArgumentsToSubRequest($subRequest);
234  $subResponse = $this->objectManager->get(Response::class);
235  $this->controller->processRequest($subRequest, $subResponse);
236  return $subResponse;
237  }
238 
244  private function ‪passArgumentsToSubRequest(‪WidgetRequest $subRequest)
245  {
246  $arguments = $this->renderingContext->getControllerContext()->getRequest()->getArguments();
247  $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
248  if (isset($arguments[$widgetIdentifier])) {
249  if (isset($arguments[$widgetIdentifier]['action'])) {
250  $subRequest->‪setControllerActionName($arguments[$widgetIdentifier]['action']);
251  unset($arguments[$widgetIdentifier]['action']);
252  }
253  $subRequest->‪setArguments($arguments[$widgetIdentifier]);
254  }
255  }
256 
263  private function ‪initializeWidgetIdentifier()
264  {
265  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
266  $widgetCounter = $viewHelperVariableContainer->get(\‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', 0);
267  $widgetIdentifier = '@widget_' . ((isset($this->arguments['customWidgetId']) && $this->arguments['customWidgetId'] !== null) ? $this->arguments['customWidgetId'] . '_' : '') . $widgetCounter;
268  $viewHelperVariableContainer->addOrUpdate(\‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper::class, 'nextWidgetNumber', $widgetCounter + 1);
269  $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
270  }
271 
280  public function ‪compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
281  {
282  $compiler->disable();
283  return '\'\'';
284  }
285 }
‪TYPO3\CMS\Fluid\Core\Widget
Definition: AbstractWidgetController.php:16
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$ajaxWidgetContextHolder
‪TYPO3 CMS Fluid Core Widget AjaxWidgetContextHolder $ajaxWidgetContextHolder
Definition: AbstractWidgetViewHelper.php:46
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$extensionService
‪TYPO3 CMS Extbase Service ExtensionService $extensionService
Definition: AbstractWidgetViewHelper.php:54
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\passArgumentsToSubRequest
‪passArgumentsToSubRequest(WidgetRequest $subRequest)
Definition: AbstractWidgetViewHelper.php:237
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$controller
‪TYPO3 CMS Fluid Core Widget AbstractWidgetController null $controller
Definition: AbstractWidgetViewHelper.php:36
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectAjaxWidgetContextHolder
‪injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
Definition: AbstractWidgetViewHelper.php:68
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
Definition: WidgetRequest.php:25
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\evaluate
‪string evaluate(RenderingContextInterface $renderingContext)
Definition: AbstractWidgetViewHelper.php:135
‪TYPO3
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\getWidgetConfiguration
‪array getWidgetConfiguration()
Definition: AbstractWidgetViewHelper.php:201
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: AbstractWidgetViewHelper.php:77
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\injectExtensionService
‪injectExtensionService(ExtensionService $extensionService)
Definition: AbstractWidgetViewHelper.php:87
‪TYPO3\CMS\Extbase\Mvc\Request\setControllerActionName
‪setControllerActionName($actionName)
Definition: Request.php:297
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeArgumentsAndRender
‪string initializeArgumentsAndRender()
Definition: AbstractWidgetViewHelper.php:120
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: AbstractWidgetViewHelper.php:58
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper
Definition: AbstractWidgetViewHelper.php:30
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$ajaxWidget
‪bool $ajaxWidget
Definition: AbstractWidgetViewHelper.php:42
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$widgetContext
‪TYPO3 CMS Fluid Core Widget WidgetContext $widgetContext
Definition: AbstractWidgetViewHelper.php:62
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:26
‪TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException
Definition: MissingControllerException.php:26
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeWidgetIdentifier
‪initializeWidgetIdentifier()
Definition: AbstractWidgetViewHelper.php:256
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: AbstractWidgetViewHelper.php:50
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractWidgetViewHelper.php:96
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\onClose
‪ComponentInterface onClose(RenderingContextInterface $renderingContext)
Definition: AbstractWidgetViewHelper.php:151
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\compile
‪string compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
Definition: AbstractWidgetViewHelper.php:273
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:33
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:33
‪TYPO3\CMS\Extbase\Mvc\Request\setArguments
‪setArguments(array $arguments)
Definition: Request.php:365
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\setChildNodes
‪setChildNodes(array $childNodes)
Definition: AbstractWidgetViewHelper.php:187
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface\get
‪object &T get(string $className,... $constructorArguments)
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initializeWidgetContext
‪initializeWidgetContext()
Definition: AbstractWidgetViewHelper.php:163
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
Definition: AbstractWidgetController.php:32
‪TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper\initiateSubRequest
‪TYPO3 CMS Extbase Mvc ResponseInterface initiateSubRequest()
Definition: AbstractWidgetViewHelper.php:213