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\Field\FieldViewHelperService; |
20
|
|
|
use Romm\Formz\Service\ViewHelper\Slot\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
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function initializeArguments() |
41
|
|
|
{ |
42
|
|
|
$this->registerArgument('slot', 'string', 'Instance of the slot which will be rendered.', true); |
43
|
|
|
$this->registerArgument('arguments', 'array', 'Arguments sent to the slot.', false, []); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function render() |
50
|
|
|
{ |
51
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Will render the slot with the given name, only if the slot is found. |
56
|
|
|
* |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
60
|
|
|
{ |
61
|
|
|
/** @var FieldViewHelperService $fieldService */ |
62
|
|
|
$fieldService = Core::instantiate(FieldViewHelperService::class); |
63
|
|
|
|
64
|
|
|
if (false === $fieldService->fieldContextExists()) { |
65
|
|
|
throw ContextNotFoundException::slotRenderViewHelperFieldContextNotFound(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @var SlotViewHelperService $slotService */ |
69
|
|
|
$slotService = Core::instantiate(SlotViewHelperService::class); |
70
|
|
|
$slotName = $arguments['slot']; |
71
|
|
|
$result = ''; |
72
|
|
|
|
73
|
|
|
if ($slotService->hasSlot($slotName)) { |
74
|
|
|
$slotService->addTemplateVariables($slotName, $arguments['arguments']); |
75
|
|
|
|
76
|
|
|
$slotClosure = $slotService->getSlotClosure($slotName); |
77
|
|
|
$result = $slotClosure(); |
78
|
|
|
|
79
|
|
|
$slotService->restoreTemplateVariables($slotName); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.