Debian Codegraph @

vendor/oojs/oojs-ui/php/layouts/FieldLayout.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Layout made of a field and optional label.
7 *
8 * Available label alignment modes include:
9 * - left: Label is before the field and aligned away from it, best for when the user will be
10 * scanning for a specific label in a form with many fields
11 * - right: Label is before the field and aligned toward it, best for forms the user is very
12 * familiar with and will tab through field checking quickly to verify which field they are in
13 * - top: Label is before the field and above it, best for when the user will need to fill out all
14 * fields from top to bottom in a form with few fields
15 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
16 * checkboxes or radio buttons
17 */
18 class FieldLayout extends Layout {
19 use LabelElement;
20 use TitledElement;
21
22 /**
23 * Alignment.
24 *
25 * @var string
26 */
27 protected $align;
28
29 /**
30 * Field widget to be laid out.
31 *
32 * @var Widget
33 */
34 protected $fieldWidget;
35
36 /**
37 * Error messages.
38 *
39 * @var array
40 */
41 protected $errors;
42
43 /**
44 * Warning messages.
45 *
46 * @var array
47 */
48 protected $warnings;
49
50 /**
51 * Success messages.
52 *
53 * @var array
54 */
55 protected $successMessages;
56
57 /**
58 * Notice messages.
59 *
60 * @var array
61 */
62 protected $notices;
63
64 /**
65 * @var ButtonWidget|string
66 */
67 protected $help;
68
69 /** @var Tag */
70 protected $field;
71 /** @var Tag */
72 protected $header;
73 /** @var Tag */
74 protected $body;
75 /** @var Tag */
76 protected $messages;
77 /** @var string */
78 protected $helpText;
79 /** @var string|false */
80 protected $helpInline;
81
82 /**
83 * @param Widget $fieldWidget Field widget. An exception is thrown if no widget is specified.
84 * @param array $config Configuration options
85 * - string $config['align'] Alignment mode, either 'left', 'right', 'top' or 'inline'
86 * (default: 'left')
87 * - string[]|HtmlSnippet[] $config['errors'] Error messages about the widget.
88 * - string[]|HtmlSnippet[] $config['warnings'] Warning messages about the widget.
89 * - string[]|HtmlSnippet[] $config['notices'] Notices about the widget.
90 * - string|HtmlSnippet $config['help'] Explanatory text shown as a '?' icon, or inline.
91 * - bool $config['helpInline'] Whether or not the help should be inline,
92 * or shown when the "help" icon is clicked. (default: false)
93 */
94 public function __construct( $fieldWidget, array $config = [] ) {
95 // Allow passing positional parameters inside the config array
96 if ( is_array( $fieldWidget ) && isset( $fieldWidget['fieldWidget'] ) ) {
97 $config = $fieldWidget;
98 $fieldWidget = $config['fieldWidget'];
99 }
100
101 // Make sure we have required constructor arguments
102 if ( $fieldWidget === null ) {
103 throw new Exception( 'Widget not found' );
104 }
105
106 // Config initialization
107 if ( ( $config['help'] ?? '' ) !== '' && ( $config['label'] ?? '' ) === '' ) {
108 // Add an empty label. For some combinations of 'helpInline' and 'align'
109 // there would be no space in the interface to display the help text otherwise.
110 $config['label'] = ' ';
111 }
112
113 // Parent constructor
114 parent::__construct( $config );
115
116 // Properties
117 $this->fieldWidget = $fieldWidget;
118 $this->errors = $config['errors'] ?? [];
119 $this->warnings = $config['warnings'] ?? [];
120 $this->successMessages = $config['successMessages'] ?? [];
121 $this->notices = $config['notices'] ?? [];
122 $this->field = $this->isFieldInline() ? new Tag( 'span' ) : new Tag( 'div' );
123 $this->messages = new Tag( 'div' );
124 $this->header = new Tag( 'span' );
125 $this->body = new Tag( 'div' );
126 $this->helpText = $config['help'] ?? '';
127 $this->helpInline = $config['helpInline'] ?? false;
128
129 // Traits
130 $this->initializeLabelElement( array_merge( [
131 'labelElement' => new Tag( 'label' )
132 ], $config ) );
133 $this->initializeTitledElement(
134 array_merge( [ 'titled' => $this->label ], $config )
135 );
136
137 // Initialization
138 $this->help = $this->helpText === '' ? '' : $this->createHelpElement();
139 if ( $this->fieldWidget->getInputId() ) {
140 $this->label->setAttributes( [ 'for' => $this->fieldWidget->getInputId() ] );
141 if ( $this->helpText !== '' && $this->helpInline ) {
142 $this->help->setAttributes( [ 'for' => $this->fieldWidget->getInputId() ] );
143 }
144 } else {
145 // We can't use `label for` with non-form elements, use `aria-labelledby` instead
146 $id = Tag::generateElementId();
147 $this->label->setAttributes( [ 'id' => $id ] );
148 $this->fieldWidget->setLabelledBy( $id );
149 }
150 $this
151 ->addClasses( [ 'oo-ui-fieldLayout' ] )
152 ->toggleClasses( [ 'oo-ui-fieldLayout-disabled' ], $this->fieldWidget->isDisabled() )
153 ->appendContent( $this->body );
154 if (
155 count( $this->errors ) ||
156 count( $this->warnings ) ||
157 count( $this->successMessages ) ||
158 count( $this->notices )
159 ) {
160 $this->appendContent( $this->messages );
161 }
162 $this->body->addClasses( [ 'oo-ui-fieldLayout-body' ] );
163 $this->header->addClasses( [ 'oo-ui-fieldLayout-header' ] );
164 $this->messages->addClasses( [ 'oo-ui-fieldLayout-messages' ] );
165 $this->field
166 ->addClasses( [ 'oo-ui-fieldLayout-field' ] )
167 ->appendContent( $this->fieldWidget );
168
169 foreach ( $this->errors as $text ) {
170 $this->messages->appendContent( $this->makeMessage( 'error', $text ) );
171 }
172 foreach ( $this->warnings as $text ) {
173 $this->messages->appendContent( $this->makeMessage( 'warning', $text ) );
174 }
175 foreach ( $this->successMessages as $text ) {
176 $this->messages->appendContent( $this->makeMessage( 'success', $text ) );
177 }
178 foreach ( $this->notices as $text ) {
179 $this->messages->appendContent( $this->makeMessage( 'notice', $text ) );
180 }
181
182 $this->setAlignment( $config['align'] ?? 'left' );
183 // Call this again to take into account the widget's accessKey
184 $this->updateTitle();
185 }
186
187 /**
188 * @param string $kind 'error', 'warning', 'success' or 'notice'
189 * @param string|HtmlSnippet $text
190 * @return Tag
191 */
192 private function makeMessage( $kind, $text ) {
193 return new MessageWidget( [
194 'type' => $kind,
195 'inline' => true,
196 'label' => $text,
197 ] );
198 }
199
200 /**
201 * Get the field.
202 *
203 * @return Widget Field widget
204 */
205 public function getField() {
206 return $this->fieldWidget;
207 }
208
209 /**
210 * Return `true` if the given field widget can be used with `'inline'` alignment (see
211 * setAlignment()). Return `false` if it can't or if this can't be determined.
212 *
213 * @return bool
214 */
215 public function isFieldInline() {
216 // This is very simplistic, but should be good enough. It's important to avoid false positives,
217 // as that will cause the generated HTML to be invalid and go all out of whack when parsed.
218 return strtolower( $this->getField()->getTag() ) === 'span';
219 }
220
221 /**
222 * Set the field alignment mode.
223 *
224 * @param string $value Alignment mode, either 'left', 'right', 'top' or 'inline'
225 * @return $this
226 */
227 protected function setAlignment( $value ) {
228 if ( $value !== $this->align ) {
229 // Default to 'left'
230 if ( !in_array( $value, [ 'left', 'right', 'top', 'inline' ] ) ) {
231 $value = 'left';
232 }
233 // Validate
234 if ( $value === 'inline' && !$this->isFieldInline() ) {
235 $value = 'top';
236 }
237 // Reorder elements
238 $this->body->clearContent();
239
240 if ( $this->helpInline ) {
241 if ( $value === 'top' ) {
242 $this->header->appendContent( $this->label );
243 $this->body->appendContent( $this->header, $this->field, $this->help );
244 } elseif ( $value === 'inline' ) {
245 $this->header->appendContent( $this->label, $this->help );
246 $this->body->appendContent( $this->field, $this->header );
247 } else {
248 $this->header->appendContent( $this->label, $this->help );
249 $this->body->appendContent( $this->header, $this->field );
250 }
251 } else {
252 if ( $value === 'top' ) {
253 $this->header->appendContent( $this->help, $this->label );
254 $this->body->appendContent( $this->header, $this->field );
255 } elseif ( $value === 'inline' ) {
256 $this->header->appendContent( $this->help, $this->label );
257 $this->body->appendContent( $this->field, $this->header );
258 } else {
259 $this->header->appendContent( $this->label );
260 $this->body->appendContent( $this->header, $this->help, $this->field );
261 }
262 }
263 // Set classes. The following classes can be used here:
264 // * oo-ui-fieldLayout-align-left
265 // * oo-ui-fieldLayout-align-right
266 // * oo-ui-fieldLayout-align-top
267 // * oo-ui-fieldLayout-align-inline
268 if ( $this->align ) {
269 $this->removeClasses( [ 'oo-ui-fieldLayout-align-' . $this->align ] );
270 }
271 $this->addClasses( [ 'oo-ui-fieldLayout-align-' . $value ] );
272 $this->align = $value;
273 }
274
275 return $this;
276 }
277
278 /**
279 * Include information about the widget's accessKey in our title. TitledElement calls this method.
280 * (This is a bit of a hack.)
281 *
282 * @param string $title Tooltip label for 'title' attribute
283 * @return string
284 */
285 protected function formatTitleWithAccessKey( $title ) {
286 if ( $this->fieldWidget && method_exists( $this->fieldWidget, 'formatTitleWithAccessKey' ) ) {
287 // @phan-suppress-next-line PhanUndeclaredMethod
288 return $this->fieldWidget->formatTitleWithAccessKey( $title );
289 }
290 return $title;
291 }
292
293 /** @inheritDoc */
294 public function getConfig( &$config ) {
295 $config['fieldWidget'] = $this->fieldWidget;
296 if ( $this->align !== 'left' ) {
297 $config['align'] = $this->align;
298 }
299 if ( count( $this->errors ) ) {
300 $config['errors'] = $this->errors;
301 }
302 if ( count( $this->warnings ) ) {
303 $config['warnings'] = $this->warnings;
304 }
305 if ( count( $this->successMessages ) ) {
306 $config['successMessages'] = $this->successMessages;
307 }
308 if ( count( $this->notices ) ) {
309 $config['notices'] = $this->notices;
310 }
311 if ( $this->helpText !== '' ) {
312 $config['help'] = $this->helpText;
313 }
314 if ( $this->helpInline ) {
315 $config['helpInline'] = $this->helpInline;
316 }
317 $config['$overlay'] = true;
318 return parent::getConfig( $config );
319 }
320
321 /**
322 * Creates and returns the help element.
323 *
324 * @return Widget The element that should become `$this->help`.
325 */
326 private function createHelpElement() {
327 if ( $this->helpInline ) {
328 return new LabelWidget( [
329 'classes' => [ 'oo-ui-inline-help' ],
330 'label' => $this->helpText,
331 ] );
332 } else {
333 return new ButtonWidget( [
334 'classes' => [ 'oo-ui-fieldLayout-help' ],
335 'framed' => false,
336 'icon' => 'info',
337 'title' => $this->helpText,
338 // TODO We have no way to use localisation messages in PHP
339 // (and to use different languages when used from MediaWiki)
340 // 'label' => msg( 'ooui-field-help' ),
341 // 'invisibleLabel' => true,
342 ] );
343 }
344 }
345 }