1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\ViewHelpers\Slot; |
15
|
|
|
|
16
|
|
|
use Closure; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use Romm\Formz\Exceptions\ContextNotFoundException; |
19
|
|
|
use Romm\Formz\Service\ViewHelper\FieldViewHelperService; |
20
|
|
|
use Romm\Formz\Service\ViewHelper\SlotViewHelperService; |
21
|
|
|
use Romm\Formz\ViewHelpers\AbstractViewHelper; |
22
|
|
|
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; |
23
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is the rendering function for the `slot` view helper. |
27
|
|
|
* |
28
|
|
|
* @see \Romm\Formz\ViewHelpers\SlotViewHelper |
29
|
|
|
*/ |
30
|
|
|
class RenderViewHelper extends AbstractViewHelper implements CompilableInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $escapeOutput = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FieldViewHelperService |
39
|
|
|
*/ |
40
|
|
|
protected $fieldService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function initializeArguments() |
46
|
|
|
{ |
47
|
|
|
$this->registerArgument('slot', 'string', 'Instance of the slot which will be rendered.', true); |
48
|
|
|
$this->registerArgument('arguments', 'array', 'Arguments sent to the slot.', false, []); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function render() |
55
|
|
|
{ |
56
|
|
|
if (false === $this->fieldService->fieldContextExists()) { |
57
|
|
|
throw ContextNotFoundException::slotRenderViewHelperFieldContextNotFound(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Will render the slot with the given name, only if the slot is found. |
65
|
|
|
* |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
69
|
|
|
{ |
70
|
|
|
/** @var SlotViewHelperService $slotService */ |
71
|
|
|
$slotService = Core::instantiate(SlotViewHelperService::class); |
72
|
|
|
$slotName = $arguments['slot']; |
73
|
|
|
$result = ''; |
74
|
|
|
|
75
|
|
|
if ($slotService->hasSlot($slotName)) { |
76
|
|
|
$slotService->addTemplateVariables($slotName, $arguments['arguments'], $renderingContext); |
77
|
|
|
|
78
|
|
|
$slotClosure = $slotService->getSlotClosure($slotName); |
79
|
|
|
$result = $slotClosure(); |
80
|
|
|
|
81
|
|
|
$slotService->restoreTemplateVariables($slotName, $renderingContext); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param FieldViewHelperService $service |
89
|
|
|
*/ |
90
|
|
|
public function injectFieldService(FieldViewHelperService $service) |
91
|
|
|
{ |
92
|
|
|
$this->fieldService = $service; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.