20 private bool $mDropdown =
false;
22 private ?
string $mPlaceholder =
null;
39 parent::__construct( $params );
42 if ( !isset( $this->mParams[
'disabled-options'] ) ) {
43 $this->mParams[
'disabled-options'] = [];
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();
55 if ( isset( $params[
'flatlist'] ) ) {
56 $this->mClass .=
' mw-htmlform-flatlist';
65 $p = parent::validate( $value, $alldata );
71 if ( !is_array( $value ) ) {
76 $value = array_filter( $value,
'is_scalar' );
78 if ( isset( $this->mParams[
'required'] )
79 && $this->mParams[
'required'] !==
false
82 return $this->
msg(
'htmlform-required' );
85 if ( isset( $this->mParams[
'max'] ) && ( count( $value ) > $this->mParams[
'max'] ) ) {
86 return $this->
msg(
'htmlform-multiselect-toomany', $this->mParams[
'max'] );
89 # If all options are valid, array_intersect of the valid options
90 # and the provided options will return the provided options.
93 $validValues = array_intersect( $value, $validOptions );
94 if ( count( $validValues ) == count( $value ) ) {
97 return $this->
msg(
'htmlform-select-badoption' );
123 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
125 foreach ( $options as $label => $info ) {
126 if ( is_array( $info ) ) {
127 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
131 'id' =>
"{$this->mID}-$info",
134 if ( in_array( $info, $this->mParams[
'disabled-options'],
true ) ) {
135 $thisAttribs[
'disabled'] =
'disabled';
137 $checked = in_array( $info, $value,
true );
139 $checkbox = $this->
getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
141 $html .=
' ' . Html::rawElement(
143 [
'class' =>
'mw-htmlform-flatlist-item' ],
154 throw new RuntimeException( __METHOD__ .
' is not supported' );
157 Html::check(
"{$this->mName}[]", $checked, $attribs ) .
161 [
'for' => $attribs[
'id'] ],
169 $optionsOouiSections = [];
174 foreach ( $options as $label => $section ) {
175 if ( is_array( $section ) ) {
176 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
177 unset( $options[$label] );
183 $optionsOouiSections = array_merge(
184 [
'' => Html::listDropdownOptionsOoui( $options ) ],
189 return $optionsOouiSections;
205 $this->mParent->getOutput()->addModules(
'oojs-ui-widgets' );
206 if ( $this->mDropdown ) {
207 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.TagMultiselectWidget.styles' );
211 $value = array_filter( $value,
'is_scalar' );
215 foreach ( $optionsSections as $sectionLabel => &$groupedOptions ) {
217 $attr[
'name'] =
"{$this->mName}[]";
219 $attr[
'value'] = $value;
221 foreach ( $groupedOptions as &$option ) {
222 $option[
'disabled'] = in_array( $option[
'data'], $this->mParams[
'disabled-options'],
true );
224 foreach ( $groupedOptions as &$option ) {
228 $attr[
'options'] = $groupedOptions;
230 $attr += \OOUI\Element::configFromHtmlAttributes(
234 if ( $this->mClass !==
'' ) {
238 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
239 if ( $sectionLabel ) {
240 $out[] = new \OOUI\FieldsetLayout( [
241 'items' => [ $widget ],
248 unset( $groupedOptions );
251 if ( $this->mPlaceholder ) {
252 $params[
'placeholder'] = $this->mPlaceholder;
254 if ( isset( $this->mParams[
'max'] ) ) {
255 $params[
'tagLimit'] = $this->mParams[
'max'];
257 if ( $this->mDropdown ) {
259 'name' => $this->mName,
260 'options' => $optionsSections,
262 'noJsFallback' => $out,
263 'allowReordering' =>
false,
265 } elseif ( count( $out ) === 1 ) {
266 $firstFieldData = $out[0]->getData() ?: [];
267 $out[0]->setData( $firstFieldData + $params );
273 return implode(
'', $out );
277 return $this->mDropdown ? [
'mediawiki.widgets.MenuTagMultiselectWidget' ] : [];
281 return $this->mDropdown;
291 $fromRequest = $request->getArray( $this->mName, [] );
311 return $this->mDefault ?? [];
321 $forcedOn = array_intersect( $this->mParams[
'disabled-options'], $this->
getDefault() );
324 foreach ( $options as $opt ) {
325 $res[
"$opt"] = in_array( $opt, $forcedOn,
true ) || in_array( $opt, $data,
true );
341class_alias( HTMLMultiSelectField::class,
'HTMLMultiSelectField' );