3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\Html\Html
;
6 use MediaWiki\HTMLForm\HTMLFormField
;
7 use MediaWiki\HTMLForm\HTMLNestedFilterable
;
8 use MediaWiki\HTMLForm\OOUIHTMLForm
;
9 use MediaWiki\Request\WebRequest
;
10 use MediaWiki\Widget\MenuTagMultiselectWidget
;
11 use MediaWiki\Xml\Xml
;
19 class HTMLMultiSelectField
extends HTMLFormField
implements HTMLNestedFilterable
{
21 private bool $mDropdown = false;
23 private ?
string $mPlaceholder = null;
28 * @param array $params
29 * In adition to the usual HTMLFormField parameters, this can take the following fields:
30 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
31 * can be used to filter them. This is desirable mostly for very long lists of options.
32 * This only works for users with JavaScript support and falls back to the list of checkboxes.
33 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
34 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
35 * for very short lists of concisely labelled options.
36 * - max: Maximum number of elements that can be selected. On the client-side, this is only
37 * enforced when using a dropdown.
39 public function __construct( $params ) {
40 parent
::__construct( $params );
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'] = [];
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();
56 if ( isset( $params['flatlist'] ) ) {
57 $this->mClass
.= ' mw-htmlform-flatlist';
65 public function validate( $value, $alldata ) {
66 $p = parent
::validate( $value, $alldata );
72 if ( !is_array( $value ) ) {
76 // Reject nested arrays (T274955)
77 $value = array_filter( $value, 'is_scalar' );
79 if ( isset( $this->mParams
['max'] ) && ( count( $value ) > $this->mParams
['max'] ) ) {
80 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams
['max'] );
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() );
87 $validValues = array_intersect( $value, $validOptions );
88 if ( count( $validValues ) == count( $value ) ) {
91 return $this->msg( 'htmlform-select-badoption' );
99 public function getInputHTML( $value ) {
100 $value = HTMLFormField
::forceToStringRecursive( $value );
101 $html = $this->formatOptions( $this->getOptions(), $value );
107 * @stable to override
109 * @param array $options
110 * @param mixed $value
114 public function formatOptions( $options, $value ) {
117 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
119 foreach ( $options as $label => $info ) {
120 if ( is_array( $info ) ) {
121 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
122 $html .= $this->formatOptions( $info, $value );
125 'id' => "{$this->mID}-$info",
128 if ( in_array( $info, $this->mParams
['disabled-options'], true ) ) {
129 $thisAttribs['disabled'] = 'disabled';
131 $checked = in_array( $info, $value, true );
133 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
135 $html .= ' ' . Html
::rawElement(
137 [ 'class' => 'mw-htmlform-flatlist-item' ],
146 protected function getOneCheckbox( $checked, $attribs, $label ) {
147 if ( $this->mParent
instanceof OOUIHTMLForm
) {
148 throw new RuntimeException( __METHOD__
. ' is not supported' );
150 $elementFunc = [ Html
::class, $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
152 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
154 call_user_func( $elementFunc,
156 [ 'for' => $attribs['id'] ],
163 public function getOptionsOOUI() {
164 $optionsOouiSections = [];
165 $options = $this->getOptions();
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] );
176 // If anything remains in the array, they are sectionless options. Put them at the beginning.
178 $optionsOouiSections = array_merge(
179 [ '' => Html
::listDropdownOptionsOoui( $options ) ],
184 return $optionsOouiSections;
188 * Get the OOUI version of this field.
190 * Returns OOUI\CheckboxMultiselectInputWidget for fields that only have one section,
193 * @stable to override
195 * @param string[] $value
196 * @return \OOUI\Widget|string
197 * @suppress PhanParamSignatureMismatch
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' );
205 // Reject nested arrays (T274955)
206 $value = array_filter( $value, 'is_scalar' );
209 $optionsSections = $this->getOptionsOOUI();
210 foreach ( $optionsSections as $sectionLabel => &$groupedOptions ) {
212 $attr['name'] = "{$this->mName}[]";
214 $attr['value'] = $value;
216 foreach ( $groupedOptions as &$option ) {
217 $option['disabled'] = in_array( $option['data'], $this->mParams
['disabled-options'], true );
219 if ( $this->mOptionsLabelsNotFromMessage
) {
220 foreach ( $groupedOptions as &$option ) {
221 $option['label'] = new \OOUI\
HtmlSnippet( $option['label'] );
225 $attr['options'] = $groupedOptions;
227 $attr +
= \OOUI\Element
::configFromHtmlAttributes(
228 $this->getAttributes( [ 'disabled', 'tabindex' ] )
231 if ( $this->mClass
!== '' ) {
232 $attr['classes'] = [ $this->mClass
];
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 ),
246 unset( $groupedOptions );
249 if ( $this->mPlaceholder
) {
250 $params['placeholder'] = $this->mPlaceholder
;
252 if ( isset( $this->mParams
['max'] ) ) {
253 $params['tagLimit'] = $this->mParams
['max'];
255 if ( $this->mDropdown
) {
256 return new MenuTagMultiselectWidget( [
257 'name' => $this->mName
,
258 'options' => $optionsSections,
260 'noJsFallback' => $out,
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.
270 return implode( '', $out );
273 protected function getOOUIModules() {
274 return $this->mDropdown ?
[ 'mediawiki.widgets.MenuTagMultiselectWidget' ] : [];
277 protected function shouldInfuseOOUI() {
278 return $this->mDropdown
;
282 * @stable to override
283 * @param WebRequest $request
285 * @return string|array
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
298 // That's ok, the user has not yet submitted the form, so show the defaults
299 return $this->getDefault();
305 * @stable to override
307 public function getDefault() {
308 return $this->mDefault ??
[];
313 * @stable to override
315 public function filterDataForSubmit( $data ) {
316 $data = HTMLFormField
::forceToStringRecursive( $data );
317 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
318 $forcedOn = array_intersect( $this->mParams
['disabled-options'], $this->getDefault() );
321 foreach ( $options as $opt ) {
322 $res["$opt"] = in_array( $opt, $forcedOn, true ) ||
in_array( $opt, $data, true );
330 * @stable to override
332 protected function needsLabel() {
337 /** @deprecated class alias since 1.42 */
338 class_alias( HTMLMultiSelectField
::class, 'HTMLMultiSelectField' );