Issues (202)

Classes/Widget/AbstractWidgetViewHelper.php (5 issues)

1
<?php
2
namespace ApacheSolrForTypo3\Solr\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 ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
18
use ApacheSolrForTypo3\Solr\Widget\WidgetRequest as SolrFluidWidgetRequest;
19
use TYPO3\CMS\Extbase\Mvc\Web\Response;
20
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
23
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper as AbstractCoreWidgetViewHelper;
24
use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
25
use TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException;
26
use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest as CoreWidgetRequest;
27
use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
28
use TYPO3\CMS\Extbase\Service\ExtensionService;
29
30
31
/**
32
 * Class AbstractWidgetViewHelper
33
 *
34
 * This is almost a exact copy of \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
35
 * because we need to switch the request object to overrule the widget prefix for url params
36
 * todo: find cleaner way to let widget listen to tx_solr[] instead of tx_solr[@widget]
37
 *
38
 * @author Frans Saris <[email protected]>
39
 * @author Timo Hund <[email protected]>
40
 */
41
abstract class AbstractWidgetViewHelper extends AbstractCoreWidgetViewHelper implements ViewHelperInterface
42
{
43
44
    /**
45
     * The Controller associated to this widget.
46
     * This needs to be filled by the individual subclass by di
47
     * annotation.
48
     *
49
     * @var AbstractWidgetController
50
     * @api
51
     */
52
    protected $controller;
53
54
    /**
55
     * If set to TRUE, it is an AJAX widget.
56
     *
57
     * @var boolean
58
     * @api
59
     */
60
    protected $ajaxWidget = false;
61
62
    /**
63
     * @var AjaxWidgetContextHolder
64
     */
65
    private $ajaxWidgetContextHolder;
66
67
    /**
68
     * @var ObjectManagerInterface
69
     */
70
    protected $objectManager;
71
72
    /**
73
     * @var ExtensionService
74
     */
75
    protected $extensionService;
76
77
    /**
78
     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
79
     */
80
    private $widgetContext;
81
82
    /**
83
     * @param ExtensionService $extensionService
84
     */
85
    public function injectExtensionService(ExtensionService $extensionService)
86 35
    {
87
        $this->extensionService = $extensionService;
88 35
    }
89 35
90
    /**
91
     * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
92
     * @return void
93
     */
94
    public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
95 35
    {
96
        $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
97 35
    }
98 35
99
    /**
100
     * @param ObjectManagerInterface $objectManager
101
     * @return void
102
     */
103
    public function injectObjectManager(ObjectManagerInterface $objectManager)
104 35
    {
105
        $this->objectManager = $objectManager;
106 35
        $this->widgetContext = $this->objectManager->get(WidgetContext::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
        $this->widgetContext = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(WidgetContext::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107 35
    }
108 35
109
    /**
110
     * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
111
     *
112
     * @return string the rendered ViewHelper.
113
     */
114
    public function initializeArgumentsAndRender()
115 34
    {
116
        $this->validateArguments();
117 34
        $this->initialize();
118 34
        $this->initializeWidgetContext();
119 34
        return $this->callRenderMethod();
120 34
    }
121
122
    /**
123
     * @return SolrControllerContext
124
     * @throws \InvalidArgumentException
125
     */
126
    protected function getControllerContext()
127
    {
128 34
        $controllerContext = null;
129
        if (method_exists($this->renderingContext, 'getControllerContext')) {
130 34
            $controllerContext = $this->renderingContext->getControllerContext();
131 34
        }
132 34
133
        if (!$controllerContext instanceof SolrControllerContext) {
134
            throw new \InvalidArgumentException('No valid SolrControllerContext found', 1512998673);
135
        }
136
137 34
        return $controllerContext;
138
    }
139
140
    /**
141 34
     * Initialize the Widget Context, before the Render method is called.
142
     *
143
     * @return void
144
     */
145
    private function initializeWidgetContext()
146
    {
147
        $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
148
        $this->initializeWidgetIdentifier();
149 34
        $this->widgetContext->setControllerObjectName(get_class($this->controller));
150
151 34
        $extensionName = $this->getControllerContext()->getRequest()->getControllerExtensionName();
152 34
        $pluginName = $this->getControllerContext()->getRequest()->getPluginName();
153 34
        $this->widgetContext->setParentExtensionName($extensionName);
154
        $this->widgetContext->setParentPluginName($pluginName);
155 34
        $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
156 34
        $this->widgetContext->setParentPluginNamespace($pluginNamespace);
157 34
        $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
158 34
159 34
        if ($this->ajaxWidget === true) {
160 34
            $this->ajaxWidgetContextHolder->store($this->widgetContext);
161 34
        }
162
    }
163 34
164
    /**
165
     * Stores the syntax tree child nodes in the Widget Context, so they can be
166 34
     * rendered with <f:widget.renderChildren> lateron.
167
     *
168
     * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
169
     * @return void
170
     */
171
    public function setChildNodes(array $childNodes)
172
    {
173
        $rootNode = $this->objectManager->get(RootNode::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

173
        $rootNode = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(RootNode::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
174
        foreach ($childNodes as $childNode) {
175 35
            $rootNode->addChildNode($childNode);
176
        }
177 35
        $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
178 35
    }
179 35
180
    /**
181 35
     * Generate the configuration for this widget. Override to adjust.
182 35
     *
183
     * @return array
184
     * @api
185
     */
186
    protected function getWidgetConfiguration()
187
    {
188
        return $this->arguments;
189
    }
190 34
191
    /**
192 34
     * Initiate a sub request to $this->controller. Make sure to fill $this->controller
193
     * via Dependency Injection.
194
     *
195
     * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface the response of this request.
196
     * @throws MissingControllerException
197
     */
198
    protected function initiateSubRequest()
199
    {
200
        if (!$this->controller instanceof AbstractWidgetController) {
0 ignored issues
show
$this->controller is always a sub-type of ApacheSolrForTypo3\Solr\...bstractWidgetController.
Loading history...
201
            if (isset($this->controller)) {
202 34
                throw new 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);
203
            }
204 34
            throw new 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);
205
        }
206
            /** @var $subRequest \ApacheSolrForTypo3\Solr\Widget\WidgetRequest */
207
        $subRequest = $this->objectManager->get(SolrFluidWidgetRequest::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

207
        $subRequest = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(SolrFluidWidgetRequest::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
208
        $subRequest->setWidgetContext($this->widgetContext);
209
210
        $this->passArgumentsToSubRequest($subRequest);
211 34
        $subResponse = $this->objectManager->get(Response::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

211
        $subResponse = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Response::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
212 34
        $this->controller->processRequest($subRequest, $subResponse);
213
        return $subResponse;
214 34
    }
215 34
216 34
    /**
217 34
     * Pass the arguments of the widget to the subrequest.
218
     *
219
     * @param CoreWidgetRequest $subRequest
220
     * @return void
221
     */
222
    private function passArgumentsToSubRequest(CoreWidgetRequest $subRequest)
223
    {
224
        $arguments = $this->getControllerContext()->getRequest()->getArguments();
225
226 34
        if (isset($arguments)) {
227
            if (isset($arguments['action'])) {
228 34
                $subRequest->setControllerActionName($arguments['action']);
229
                unset($arguments['action']);
230 34
            }
231 34
            $subRequest->setArguments($arguments);
232
        }
233
    }
234
235 34
    /**
236
     * The widget identifier is unique on the current page, and is used
237 34
     * in the URI as a namespace for the widget's arguments.
238
     *
239
     * @return string the widget identifier for this widget
240
     * @return void
241
     * @todo clean up, and make it somehow more routing compatible.
242
     */
243
    private function initializeWidgetIdentifier()
244
    {
245
        $this->widgetContext->setWidgetIdentifier('');
246
    }
247
}
248