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
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function render() |
54
|
|
|
{ |
55
|
|
|
if (false === $this->fieldService->fieldContextExists()) { |
56
|
|
|
throw ContextNotFoundException::slotRenderViewHelperFieldContextNotFound(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Will render the slot with the given name, only if the slot is found. |
64
|
|
|
* |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
68
|
|
|
{ |
69
|
|
|
/** @var SlotViewHelperService $slotService */ |
70
|
|
|
$slotService = Core::instantiate(SlotViewHelperService::class); |
71
|
|
|
$slotName = $arguments['slot']; |
72
|
|
|
$result = ''; |
73
|
|
|
|
74
|
|
|
if ($slotService->hasSlotClosure($slotName)) { |
75
|
|
|
$closure = $slotService->getSlotClosure($slotName); |
76
|
|
|
$result = $closure(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param FieldViewHelperService $service |
84
|
|
|
*/ |
85
|
|
|
public function injectFieldService(FieldViewHelperService $service) |
86
|
|
|
{ |
87
|
|
|
$this->fieldService = $service; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.