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; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
17
|
|
|
use Romm\Formz\Exceptions\ContextNotFoundException; |
18
|
|
|
use Romm\Formz\Service\ViewHelper\Field\FieldViewHelperService; |
19
|
|
|
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; |
20
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Use this view helper at the first level inside the Field view helper. It will |
24
|
|
|
* be added to the arguments list, which can then be used inside the field |
25
|
|
|
* template. |
26
|
|
|
* |
27
|
|
|
* It does not add any feature, but allows the Fluid template to be far more |
28
|
|
|
* readable, as you can see below: |
29
|
|
|
* |
30
|
|
|
* Without the Option view helper: |
31
|
|
|
* |
32
|
|
|
* ``` |
33
|
|
|
* <fz:field layout="..." |
34
|
|
|
* arguments="{label: '{f:translate(key: \'my_lll_key\')}', foo: 'bar'}"> |
35
|
|
|
* |
36
|
|
|
* ... |
37
|
|
|
* </fz:field> |
38
|
|
|
* ``` |
39
|
|
|
* |
40
|
|
|
* With it: |
41
|
|
|
* |
42
|
|
|
* ``` |
43
|
|
|
* <fz:field layout="..."> |
44
|
|
|
* <fz:option name="label" value="{f:translate(key: 'my_lll_key')}" /> |
45
|
|
|
* <fz:option name="foo" value="bar" /> |
46
|
|
|
* |
47
|
|
|
* ... |
48
|
|
|
* </fz:field> |
49
|
|
|
* ``` |
50
|
|
|
*/ |
51
|
|
|
class OptionViewHelper extends AbstractViewHelper implements CompilableInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected static $options = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function initializeArguments() |
62
|
|
|
{ |
63
|
|
|
parent::initializeArguments(); |
64
|
|
|
|
65
|
|
|
$this->registerArgument('name', 'string', 'Name of the option.', true); |
66
|
|
|
$this->registerArgument('value', 'string', 'Value of the option.', true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function render() |
73
|
|
|
{ |
74
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
81
|
|
|
{ |
82
|
|
|
/** @var FieldViewHelperService $service */ |
83
|
|
|
$service = Core::instantiate(FieldViewHelperService::class); |
84
|
|
|
|
85
|
|
|
if (false === $service->fieldContextExists()) { |
86
|
|
|
throw ContextNotFoundException::optionViewHelperFieldContextNotFound(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$service->setFieldOption($arguments['name'], $arguments['value']); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.