MediaWiki master
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
4
12use RuntimeException;
13
20
21 private bool $mDropdown = false;
22
23 private ?string $mPlaceholder = null;
24
39 public function __construct( $params ) {
40 parent::__construct( $params );
41
42 // If the disabled-options parameter is not provided, use an empty array
43 if ( !isset( $this->mParams['disabled-options'] ) ) {
44 $this->mParams['disabled-options'] = [];
45 }
46
47 if ( isset( $params['dropdown'] ) ) {
48 $this->mDropdown = true;
49 if ( isset( $params['placeholder'] ) ) {
50 $this->mPlaceholder = $params['placeholder'];
51 } elseif ( isset( $params['placeholder-message'] ) ) {
52 $this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text();
53 }
54 }
55
56 if ( isset( $params['flatlist'] ) ) {
57 $this->mClass .= ' mw-htmlform-flatlist';
58 }
59 }
60
65 public function validate( $value, $alldata ) {
66 $p = parent::validate( $value, $alldata );
67
68 if ( $p !== true ) {
69 return $p;
70 }
71
72 if ( !is_array( $value ) ) {
73 return false;
74 }
75
76 // Reject nested arrays (T274955)
77 $value = array_filter( $value, 'is_scalar' );
78
79 if ( isset( $this->mParams['max'] ) && ( count( $value ) > $this->mParams['max'] ) ) {
80 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
81 }
82
83 # If all options are valid, array_intersect of the valid options
84 # and the provided options will return the provided options.
85 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
86
87 $validValues = array_intersect( $value, $validOptions );
88 if ( count( $validValues ) == count( $value ) ) {
89 return true;
90 } else {
91 return $this->msg( 'htmlform-select-badoption' );
92 }
93 }
94
99 public function getInputHTML( $value ) {
100 $value = HTMLFormField::forceToStringRecursive( $value );
101 $html = $this->formatOptions( $this->getOptions(), $value );
102
103 return $html;
104 }
105
114 public function formatOptions( $options, $value ) {
115 $html = '';
116
117 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
118
119 foreach ( $options as $label => $info ) {
120 if ( is_array( $info ) ) {
121 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
122 $html .= $this->formatOptions( $info, $value );
123 } else {
124 $thisAttribs = [
125 'id' => "{$this->mID}-$info",
126 'value' => $info,
127 ];
128 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
129 $thisAttribs['disabled'] = 'disabled';
130 }
131 $checked = in_array( $info, $value, true );
132
133 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
134
135 $html .= ' ' . Html::rawElement(
136 'div',
137 [ 'class' => 'mw-htmlform-flatlist-item' ],
138 $checkbox
139 );
140 }
141 }
142
143 return $html;
144 }
145
146 protected function getOneCheckbox( $checked, $attribs, $label ) {
147 if ( $this->mParent instanceof OOUIHTMLForm ) {
148 throw new RuntimeException( __METHOD__ . ' is not supported' );
149 } else {
150 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
151 $checkbox =
152 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
153 "\u{00A0}" .
154 call_user_func( $elementFunc,
155 'label',
156 [ 'for' => $attribs['id'] ],
157 $label
158 );
159 return $checkbox;
160 }
161 }
162
163 public function getOptionsOOUI() {
164 $optionsOouiSections = [];
165 $options = $this->getOptions();
166
167 // If the options are supposed to be split into sections, each section becomes a separate
168 // CheckboxMultiselectInputWidget.
169 foreach ( $options as $label => $section ) {
170 if ( is_array( $section ) ) {
171 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
172 unset( $options[$label] );
173 }
174 }
175
176 // If anything remains in the array, they are sectionless options. Put them at the beginning.
177 if ( $options ) {
178 $optionsOouiSections = array_merge(
179 [ '' => Html::listDropdownOptionsOoui( $options ) ],
180 $optionsOouiSections
181 );
182 }
183
184 return $optionsOouiSections;
185 }
186
199 public function getInputOOUI( $value ) {
200 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
201 if ( $this->mDropdown ) {
202 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
203 }
204
205 // Reject nested arrays (T274955)
206 $value = array_filter( $value, 'is_scalar' );
207
208 $out = [];
209 $optionsSections = $this->getOptionsOOUI();
210 foreach ( $optionsSections as $sectionLabel => &$groupedOptions ) {
211 $attr = [];
212 $attr['name'] = "{$this->mName}[]";
213
214 $attr['value'] = $value;
215
216 foreach ( $groupedOptions as &$option ) {
217 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
218 }
219 if ( $this->mOptionsLabelsNotFromMessage ) {
220 foreach ( $groupedOptions as &$option ) {
221 $option['label'] = new \OOUI\HtmlSnippet( $option['label'] );
222 }
223 }
224 unset( $option );
225 $attr['options'] = $groupedOptions;
226
227 $attr += \OOUI\Element::configFromHtmlAttributes(
228 $this->getAttributes( [ 'disabled', 'tabindex' ] )
229 );
230
231 if ( $this->mClass !== '' ) {
232 $attr['classes'] = [ $this->mClass ];
233 }
234
235 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
236 if ( $sectionLabel ) {
237 $out[] = new \OOUI\FieldsetLayout( [
238 'items' => [ $widget ],
239 // @phan-suppress-next-line SecurityCheck-XSS Key is html, taint cannot track that
240 'label' => new \OOUI\HtmlSnippet( $sectionLabel ),
241 ] );
242 } else {
243 $out[] = $widget;
244 }
245 }
246 unset( $groupedOptions );
247
248 $params = [];
249 if ( $this->mPlaceholder ) {
250 $params['placeholder'] = $this->mPlaceholder;
251 }
252 if ( isset( $this->mParams['max'] ) ) {
253 $params['tagLimit'] = $this->mParams['max'];
254 }
255 if ( $this->mDropdown ) {
256 return new MenuTagMultiselectWidget( [
257 'name' => $this->mName,
258 'options' => $optionsSections,
259 'default' => $value,
260 'noJsFallback' => $out,
261 ] + $params );
262 } elseif ( count( $out ) === 1 ) {
263 $firstFieldData = $out[0]->getData() ?: [];
264 $out[0]->setData( $firstFieldData + $params );
265 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
266 // This allows it to be made infusable and later tweaked by JS code.
267 return $out[0];
268 }
269
270 return implode( '', $out );
271 }
272
273 protected function getOOUIModules() {
274 return $this->mDropdown ? [ 'mediawiki.widgets.MenuTagMultiselectWidget' ] : [];
275 }
276
277 protected function shouldInfuseOOUI() {
278 return $this->mDropdown;
279 }
280
287 public function loadDataFromRequest( $request ) {
288 $fromRequest = $request->getArray( $this->mName, [] );
289 // Fetch the value in either one of the two following case:
290 // - we have a valid submit attempt (form was just submitted)
291 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
292 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
293 // Checkboxes are just not added to the request arrays if they're not checked,
294 // so it's perfectly possible for there not to be an entry at all
295 // @phan-suppress-next-line PhanTypeMismatchReturnNullable getArray does not return null
296 return $fromRequest;
297 } else {
298 // That's ok, the user has not yet submitted the form, so show the defaults
299 return $this->getDefault();
300 }
301 }
302
307 public function getDefault() {
308 return $this->mDefault ?? [];
309 }
310
315 public function filterDataForSubmit( $data ) {
317 $options = HTMLFormField::flattenOptions( $this->getOptions() );
318 $forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() );
319
320 $res = [];
321 foreach ( $options as $opt ) {
322 $res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true );
323 }
324
325 return $res;
326 }
327
332 protected function needsLabel() {
333 return false;
334 }
335}
336
338class_alias( HTMLMultiSelectField::class, 'HTMLMultiSelectField' );
array $params
The job parameters.
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.
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...
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:56
static check( $name, $checked=false, array $attribs=[])
Convenience function to produce a checkbox (input element with type=checkbox)
Definition Html.php:672
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Module of static functions for generating XML.
Definition Xml.php:37