MediaWiki master
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
4
11use RuntimeException;
12
19
20 private bool $mDropdown = false;
21
22 private ?string $mPlaceholder = null;
23
38 public function __construct( $params ) {
39 parent::__construct( $params );
40
41 // If the disabled-options parameter is not provided, use an empty array
42 if ( !isset( $this->mParams['disabled-options'] ) ) {
43 $this->mParams['disabled-options'] = [];
44 }
45
46 if ( isset( $params['dropdown'] ) ) {
47 $this->mDropdown = true;
48 if ( isset( $params['placeholder'] ) ) {
49 $this->mPlaceholder = $params['placeholder'];
50 } elseif ( isset( $params['placeholder-message'] ) ) {
51 $this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text();
52 }
53 }
54
55 if ( isset( $params['flatlist'] ) ) {
56 $this->mClass .= ' mw-htmlform-flatlist';
57 }
58 }
59
64 public function validate( $value, $alldata ) {
65 $p = parent::validate( $value, $alldata );
66
67 if ( $p !== true ) {
68 return $p;
69 }
70
71 if ( !is_array( $value ) ) {
72 return false;
73 }
74
75 // Reject nested arrays (T274955)
76 $value = array_filter( $value, 'is_scalar' );
77
78 if ( isset( $this->mParams['required'] )
79 && $this->mParams['required'] !== false
80 && $value === []
81 ) {
82 return $this->msg( 'htmlform-required' );
83 }
84
85 if ( isset( $this->mParams['max'] ) && ( count( $value ) > $this->mParams['max'] ) ) {
86 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
87 }
88
89 # If all options are valid, array_intersect of the valid options
90 # and the provided options will return the provided options.
91 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
92
93 $validValues = array_intersect( $value, $validOptions );
94 if ( count( $validValues ) == count( $value ) ) {
95 return true;
96 } else {
97 return $this->msg( 'htmlform-select-badoption' );
98 }
99 }
100
105 public function getInputHTML( $value ) {
106 $value = HTMLFormField::forceToStringRecursive( $value );
107 $html = $this->formatOptions( $this->getOptions(), $value );
108
109 return $html;
110 }
111
120 public function formatOptions( $options, $value ) {
121 $html = '';
122
123 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
124
125 foreach ( $options as $label => $info ) {
126 if ( is_array( $info ) ) {
127 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
128 $html .= $this->formatOptions( $info, $value );
129 } else {
130 $thisAttribs = [
131 'id' => "{$this->mID}-$info",
132 'value' => $info,
133 ];
134 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
135 $thisAttribs['disabled'] = 'disabled';
136 }
137 $checked = in_array( $info, $value, true );
138
139 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
140
141 $html .= ' ' . Html::rawElement(
142 'div',
143 [ 'class' => 'mw-htmlform-flatlist-item' ],
144 $checkbox
145 );
146 }
147 }
148
149 return $html;
150 }
151
152 protected function getOneCheckbox( $checked, $attribs, $label ) {
153 if ( $this->mParent instanceof OOUIHTMLForm ) {
154 throw new RuntimeException( __METHOD__ . ' is not supported' );
155 } else {
156 $checkbox =
157 Html::check( "{$this->mName}[]", $checked, $attribs ) .
158 "\u{00A0}" .
159 Html::rawElement(
160 'label',
161 [ 'for' => $attribs['id'] ],
162 $this->escapeLabel( $label )
163 );
164 return $checkbox;
165 }
166 }
167
168 public function getOptionsOOUI() {
169 $optionsOouiSections = [];
170 $options = $this->getOptions();
171
172 // If the options are supposed to be split into sections, each section becomes a separate
173 // CheckboxMultiselectInputWidget.
174 foreach ( $options as $label => $section ) {
175 if ( is_array( $section ) ) {
176 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
177 unset( $options[$label] );
178 }
179 }
180
181 // If anything remains in the array, they are sectionless options. Put them at the beginning.
182 if ( $options ) {
183 $optionsOouiSections = array_merge(
184 [ '' => Html::listDropdownOptionsOoui( $options ) ],
185 $optionsOouiSections
186 );
187 }
188
189 return $optionsOouiSections;
190 }
191
204 public function getInputOOUI( $value ) {
205 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
206 if ( $this->mDropdown ) {
207 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
208 }
209
210 // Reject nested arrays (T274955)
211 $value = array_filter( $value, 'is_scalar' );
212
213 $out = [];
214 $optionsSections = $this->getOptionsOOUI();
215 foreach ( $optionsSections as $sectionLabel => &$groupedOptions ) {
216 $attr = [];
217 $attr['name'] = "{$this->mName}[]";
218
219 $attr['value'] = $value;
220
221 foreach ( $groupedOptions as &$option ) {
222 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
223 }
224 foreach ( $groupedOptions as &$option ) {
225 $option['label'] = $this->makeLabelSnippet( $option['label'] );
226 }
227 unset( $option );
228 $attr['options'] = $groupedOptions;
229
230 $attr += \OOUI\Element::configFromHtmlAttributes(
231 $this->getAttributes( [ 'disabled', 'tabindex' ] )
232 );
233
234 if ( $this->mClass !== '' ) {
235 $attr['classes'] = [ $this->mClass ];
236 }
237
238 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
239 if ( $sectionLabel ) {
240 $out[] = new \OOUI\FieldsetLayout( [
241 'items' => [ $widget ],
242 'label' => $this->makeLabelSnippet( $sectionLabel ),
243 ] );
244 } else {
245 $out[] = $widget;
246 }
247 }
248 unset( $groupedOptions );
249
250 $params = [];
251 if ( $this->mPlaceholder ) {
252 $params['placeholder'] = $this->mPlaceholder;
253 }
254 if ( isset( $this->mParams['max'] ) ) {
255 $params['tagLimit'] = $this->mParams['max'];
256 }
257 if ( $this->mDropdown ) {
258 return new MenuTagMultiselectWidget( [
259 'name' => $this->mName,
260 'options' => $optionsSections,
261 'default' => $value,
262 'noJsFallback' => $out,
263 'allowReordering' => false,
264 ] + $params );
265 } elseif ( count( $out ) === 1 ) {
266 $firstFieldData = $out[0]->getData() ?: [];
267 $out[0]->setData( $firstFieldData + $params );
268 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
269 // This allows it to be made infusable and later tweaked by JS code.
270 return $out[0];
271 }
272
273 return implode( '', $out );
274 }
275
276 protected function getOOUIModules() {
277 return $this->mDropdown ? [ 'mediawiki.widgets.MenuTagMultiselectWidget' ] : [];
278 }
279
280 protected function shouldInfuseOOUI() {
281 return $this->mDropdown;
282 }
283
290 public function loadDataFromRequest( $request ) {
291 $fromRequest = $request->getArray( $this->mName, [] );
292 // Fetch the value in either one of the two following case:
293 // - we have a valid submit attempt (form was just submitted)
294 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
295 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
296 // Checkboxes are just not added to the request arrays if they're not checked,
297 // so it's perfectly possible for there not to be an entry at all
298 // @phan-suppress-next-line PhanTypeMismatchReturnNullable getArray does not return null
299 return $fromRequest;
300 } else {
301 // That's ok, the user has not yet submitted the form, so show the defaults
302 return $this->getDefault();
303 }
304 }
305
310 public function getDefault() {
311 return $this->mDefault ?? [];
312 }
313
318 public function filterDataForSubmit( $data ) {
320 $options = HTMLFormField::flattenOptions( $this->getOptions() );
321 $forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() );
322
323 $res = [];
324 foreach ( $options as $opt ) {
325 $res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true );
326 }
327
328 return $res;
329 }
330
335 protected function needsLabel() {
336 return false;
337 }
338}
339
341class_alias( HTMLMultiSelectField::class, 'HTMLMultiSelectField' );
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
shouldInfuseOOUI()
Whether the field should be automatically infused.
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Get the OOUI version of this field.
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
The parent class to generate form fields.
makeLabelSnippet( $label)
The keys in the array returned by getOptions() can be either HTML or plain text depending on $this->m...
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
getOptions()
Fetch the array of options from the field's parameters.
getAttributes(array $list)
Returns the given attributes from the parameters.
msg( $key,... $params)
Get a translated interface message.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
escapeLabel( $label)
The keys in the array returned by getOptions() can be either HTML or plain text depending on $this->m...
Compact stacked vertical format for forms, implemented using OOUI widgets.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...