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\AssetHandler\Html\DataAttributesAssetHandler; |
17
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
18
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
19
|
|
|
use TYPO3\CMS\Extbase\Error\Error; |
20
|
|
|
use TYPO3\CMS\Extbase\Error\Message; |
21
|
|
|
use TYPO3\CMS\Extbase\Error\Notice; |
22
|
|
|
use TYPO3\CMS\Extbase\Error\Warning; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This view helper can format the validation result messages of a field. |
26
|
|
|
* |
27
|
|
|
* It will use the message template defined for the given field, and handle |
28
|
|
|
* every dynamic value which can be found in the template (see below): |
29
|
|
|
* |
30
|
|
|
* #FIELD# : Name of the field; |
31
|
|
|
* #FIELD_ID# : Value of the `id` attribute of the field DOM element; |
32
|
|
|
* #VALIDATOR# : Name of the validation rule; |
33
|
|
|
* #TYPE#' : Type of the message (usually `error`); |
34
|
|
|
* #KEY#' : Key of the message (usually `default`); |
35
|
|
|
* #MESSAGE# : The message itself. |
36
|
|
|
*/ |
37
|
|
|
class FormatMessageViewHelper extends AbstractViewHelper |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function initializeArguments() |
44
|
|
|
{ |
45
|
|
|
$this->registerArgument('message', 'object', 'The message which will be formatted.', true); |
46
|
|
|
$this->registerArgument('field', 'string', 'Name of the field which will be managed. By default, it is the field from the current `FieldViewHelper`.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function render() |
53
|
|
|
{ |
54
|
|
|
$message = $this->getMessage(); |
55
|
|
|
$messageType = $this->getMessageType($message); |
56
|
|
|
$fieldName = $this->getFieldName(); |
57
|
|
|
$field = $this->getField(); |
58
|
|
|
$formObject = $this->service->getFormObject(); |
59
|
|
|
|
60
|
|
|
$validationName = ($message instanceof FormzMessageInterface) |
61
|
|
|
? $message->getValidationName() |
62
|
|
|
: 'unknown'; |
63
|
|
|
|
64
|
|
|
$messageKey = ($message instanceof FormzMessageInterface) |
65
|
|
|
? $message->getMessageKey() |
66
|
|
|
: 'unknown'; |
67
|
|
|
|
68
|
|
|
$templateVariableContainer = $this->renderingContext->getTemplateVariableContainer(); |
69
|
|
|
|
70
|
|
|
$fieldId = ($templateVariableContainer->exists('fieldId')) |
71
|
|
|
? $templateVariableContainer->get('fieldId') |
72
|
|
|
: DataAttributesAssetHandler::getFieldCleanName('formz-' . $formObject->getName() . '-' . $fieldName); |
73
|
|
|
|
74
|
|
|
$result = str_replace( |
75
|
|
|
[ |
76
|
|
|
'#FIELD#', |
77
|
|
|
'#FIELD_ID#', |
78
|
|
|
'#VALIDATOR#', |
79
|
|
|
'#TYPE#', |
80
|
|
|
'#KEY#', |
81
|
|
|
'#MESSAGE#' |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
$fieldName, |
85
|
|
|
$fieldId, |
86
|
|
|
$validationName, |
87
|
|
|
$messageType, |
88
|
|
|
$messageKey, |
89
|
|
|
$message->getMessage() |
|
|
|
|
90
|
|
|
], |
91
|
|
|
$field->getSettings()->getMessageTemplate() |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Message|FormzMessageInterface |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
protected function getMessage() |
102
|
|
|
{ |
103
|
|
|
$message = $this->arguments['message']; |
104
|
|
|
|
105
|
|
|
if (false === $message instanceof Message) { |
|
|
|
|
106
|
|
|
throw new \Exception('The argument "message" for the view helper "' . __CLASS__ . '" must be an instance of "' . Message::class . '".', 1467021406); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $message; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Message $message |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function getMessageType(Message $message) |
117
|
|
|
{ |
118
|
|
|
if ($message instanceof Error) { |
|
|
|
|
119
|
|
|
$messageType = 'error'; |
120
|
|
|
} elseif ($message instanceof Warning) { |
|
|
|
|
121
|
|
|
$messageType = 'warning'; |
122
|
|
|
} elseif ($message instanceof Notice) { |
|
|
|
|
123
|
|
|
$messageType = 'notice'; |
124
|
|
|
} else { |
125
|
|
|
$messageType = 'message'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $messageType; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
|
|
protected function getFieldName() |
136
|
|
|
{ |
137
|
|
|
$fieldName = $this->arguments['field']; |
138
|
|
|
|
139
|
|
|
if (null === $fieldName |
140
|
|
|
&& null !== $this->service->getCurrentField() |
141
|
|
|
) { |
142
|
|
|
$field = $this->service->getCurrentField(); |
143
|
|
|
$fieldName = $field->getFieldName(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null === $fieldName) { |
147
|
|
|
throw new \Exception( |
148
|
|
|
'The field could not be fetched, 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.', |
149
|
|
|
1467624152 |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $fieldName; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return Field |
158
|
|
|
* @throws \Exception |
159
|
|
|
*/ |
160
|
|
|
protected function getField() |
161
|
|
|
{ |
162
|
|
|
$formObject = $this->service->getFormObject(); |
163
|
|
|
$fieldName = $this->getFieldName(); |
164
|
|
|
|
165
|
|
|
if (false === $formObject->getConfiguration()->hasField($fieldName)) { |
166
|
|
|
throw new \Exception( |
167
|
|
|
vsprintf( |
168
|
|
|
'The Form "%s" does not have a field "%s"', |
169
|
|
|
[$formObject->getName(), $fieldName] |
170
|
|
|
), |
171
|
|
|
1473084335 |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $formObject->getConfiguration()->getField($fieldName); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.