1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\Configuration\Form\Field\Field; |
17
|
|
|
use Romm\Formz\Configuration\View\Classes\ViewClass; |
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
19
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
20
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This view helper is used to manage the properties set in TypoScript at the |
24
|
|
|
* path `config.tx_formz.view.classes`. |
25
|
|
|
* |
26
|
|
|
* Two groups of classes are handled: `errors` and `valid`. The classes will be |
27
|
|
|
* "activated" only when the field has been validated, and the result matches |
28
|
|
|
* the class group. |
29
|
|
|
* |
30
|
|
|
* In the option `name`, you must indicate which class from which group you want |
31
|
|
|
* to manage, for example `errors.has-error` for the class `has-error` from the |
32
|
|
|
* group `errors`. |
33
|
|
|
* |
34
|
|
|
* If the field currently being rendered with Fluid is not using the view helper |
35
|
|
|
* `FieldViewHelper` (all its skeleton is written manually), you may have to use |
36
|
|
|
* the option `field`, which should then contain the name of the field. |
37
|
|
|
* |
38
|
|
|
* Please be aware that this view helper is useful only when used at the same |
39
|
|
|
* level or under the HTML element containing the field selector (usually the |
40
|
|
|
* one with the data attribute `formz-field-container`). You may encounter |
41
|
|
|
* strange behaviours if you do not respect this requirement. |
42
|
|
|
*/ |
43
|
|
|
class ClassViewHelper extends AbstractViewHelper |
44
|
|
|
{ |
45
|
|
|
const CLASS_ERRORS = 'errors'; |
46
|
|
|
const CLASS_VALID = 'valid'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected static $acceptedClassesNameSpace = [self::CLASS_ERRORS, self::CLASS_VALID]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Field |
55
|
|
|
*/ |
56
|
|
|
protected $fieldName; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $classValue; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $classNameSpace; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $className; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function initializeArguments() |
77
|
|
|
{ |
78
|
|
|
$this->registerArgument('name', 'string', 'Name of the class which will be managed.', true); |
79
|
|
|
$this->registerArgument('field', 'string', 'Name of the field which will be managed. By default, it is the field from the current `FieldViewHelper`.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function render() |
86
|
|
|
{ |
87
|
|
|
$this->initializeClassNames(); |
88
|
|
|
$this->initializeClassValue(); |
89
|
|
|
$this->initializeFieldName(); |
90
|
|
|
|
91
|
|
|
$result = 'formz-' . $this->classNameSpace . '-' . str_replace(' ', '-', $this->classValue); |
92
|
|
|
|
93
|
|
|
if ($this->service->formWasSubmitted()) { |
94
|
|
|
$propertyResult = $this->getRequestResultForProperty($this->fieldName); |
95
|
|
|
|
96
|
|
|
if (null !== $propertyResult) { |
97
|
|
|
switch ($this->classNameSpace) { |
98
|
|
|
case self::CLASS_ERRORS: |
99
|
|
|
if (true === $propertyResult->hasErrors()) { |
100
|
|
|
$result .= ' ' . $this->classValue; |
101
|
|
|
} |
102
|
|
|
break; |
103
|
|
|
case self::CLASS_VALID: |
104
|
|
|
if (false === $propertyResult->hasErrors()) { |
105
|
|
|
$result .= ' ' . $this->classValue; |
106
|
|
|
} |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Will initialize the namespace and name of the class which is given as |
117
|
|
|
* argument to this ViewHelper. |
118
|
|
|
* |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
protected function initializeClassNames() |
122
|
|
|
{ |
123
|
|
|
list($this->classNameSpace, $this->className) = GeneralUtility::trimExplode('.', $this->arguments['name']); |
124
|
|
|
|
125
|
|
|
if (false === in_array($this->classNameSpace, self::$acceptedClassesNameSpace)) { |
126
|
|
|
throw new \Exception( |
127
|
|
|
'The class "' . $this->arguments['name'] . '" is not valid: the namespace of the error must be one of the following: ' . implode(', ', self::$acceptedClassesNameSpace) . '.', |
128
|
|
|
1467623504 |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Fetches the name of the field which should refer to this class. It can |
135
|
|
|
* either be a given value, or be empty if the ViewHelper is used inside a |
136
|
|
|
* `FieldViewHelper`. |
137
|
|
|
* |
138
|
|
|
* @throws \Exception |
139
|
|
|
*/ |
140
|
|
|
protected function initializeFieldName() |
141
|
|
|
{ |
142
|
|
|
$this->fieldName = $this->arguments['field']; |
143
|
|
|
|
144
|
|
|
if (null === $this->fieldName |
145
|
|
|
&& $this->service->fieldContextExists($this->renderingContext) |
146
|
|
|
) { |
147
|
|
|
$this->fieldName = $this->service |
148
|
|
|
->getCurrentField($this->renderingContext) |
149
|
|
|
->getFieldName(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (null === $this->fieldName) { |
153
|
|
|
throw new \Exception( |
154
|
|
|
'The field could not be fetched for the class "' . $this->arguments['name'] . '": please either use this view helper inside the view helper "' . FieldViewHelper::class . '", or fill the parameter "field" of this view helper with the field name you want.', |
155
|
|
|
1467623761 |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Fetches the corresponding value of this class, which was defined in |
162
|
|
|
* TypoScript. |
163
|
|
|
* |
164
|
|
|
* @throws \Exception |
165
|
|
|
*/ |
166
|
|
|
protected function initializeClassValue() |
167
|
|
|
{ |
168
|
|
|
/** @var FormViewHelper $form */ |
169
|
|
|
$form = FormViewHelper::getVariable(FormViewHelper::FORM_VIEW_HELPER); |
170
|
|
|
$classesConfiguration = $form->getFormzConfiguration()->getView()->getClasses(); |
171
|
|
|
|
172
|
|
|
/** @var ViewClass $class */ |
173
|
|
|
$class = ObjectAccess::getProperty($classesConfiguration, $this->classNameSpace); |
174
|
|
|
|
175
|
|
|
if (false === $class->hasItem($this->className)) { |
176
|
|
|
throw new \Exception( |
177
|
|
|
'The class "' . $this->arguments['name'] . '" is not valid: the class name "' . $this->className . '" was not found in the namespace "' . $this->classNameSpace . '".', |
178
|
|
|
1467623662 |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->classValue = $class->getItem($this->className); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns the result for the given property only if the current request has |
187
|
|
|
* a result for the form. |
188
|
|
|
* |
189
|
|
|
* @param string $property |
190
|
|
|
* @return Result|null |
191
|
|
|
*/ |
192
|
|
|
protected function getRequestResultForProperty($property) |
193
|
|
|
{ |
194
|
|
|
/** @var Result $requestResult */ |
195
|
|
|
$requestResult = FormViewHelper::getVariable(FormViewHelper::FORM_RESULT); |
196
|
|
|
|
197
|
|
|
return (false !== $requestResult) |
198
|
|
|
? $requestResult->forProperty($property) |
199
|
|
|
: null; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.