21 private bool $mDropdown =
false;
23 private ?
string $mPlaceholder =
null;
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';
66 $p = parent::validate( $value, $alldata );
72 if ( !is_array( $value ) ) {
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.
87 $validValues = array_intersect( $value, $validOptions );
88 if ( count( $validValues ) == count( $value ) ) {
91 return $this->
msg(
'htmlform-select-badoption' );
117 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
119 foreach ( $options as $label => $info ) {
120 if ( is_array( $info ) ) {
121 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
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' ],
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'] ],
164 $optionsOouiSections = [];
169 foreach ( $options as $label => $section ) {
170 if ( is_array( $section ) ) {
171 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
172 unset( $options[$label] );
178 $optionsOouiSections = array_merge(
179 [
'' => Html::listDropdownOptionsOoui( $options ) ],
184 return $optionsOouiSections;
200 $this->mParent->getOutput()->addModules(
'oojs-ui-widgets' );
201 if ( $this->mDropdown ) {
202 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.TagMultiselectWidget.styles' );
206 $value = array_filter( $value,
'is_scalar' );
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(
231 if ( $this->mClass !==
'' ) {
235 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
236 if ( $sectionLabel ) {
237 $out[] = new \OOUI\FieldsetLayout( [
238 'items' => [ $widget ],
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 ) {
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 );
270 return implode(
'', $out );
274 return $this->mDropdown ? [
'mediawiki.widgets.MenuTagMultiselectWidget' ] : [];
278 return $this->mDropdown;
288 $fromRequest = $request->getArray( $this->mName, [] );
308 return $this->mDefault ?? [];
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 );
338class_alias( HTMLMultiSelectField::class,
'HTMLMultiSelectField' );
array $params
The job parameters.