6 class HTMLMultiSelectField
extends HTMLFormField
implements HTMLNestedFilterable
{
9 * In adition to the usual HTMLFormField parameters, this can take the following fields:
10 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
11 * can be used to filter them. This is desirable mostly for very long lists of options.
12 * This only works for users with JavaScript support and falls back to the list of checkboxes.
13 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
14 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
15 * for very short lists of concisely labelled options.
17 public function __construct( $params ) {
18 parent
::__construct( $params );
20 // If the disabled-options parameter is not provided, use an empty array
21 if ( isset( $this->mParams
['disabled-options'] ) === false ) {
22 $this->mParams
['disabled-options'] = [];
25 if ( isset( $params['dropdown'] ) ) {
26 $this->mClass
.= ' mw-htmlform-dropdown';
29 if ( isset( $params['flatlist'] ) ) {
30 $this->mClass
.= ' mw-htmlform-flatlist';
34 public function validate( $value, $alldata ) {
35 $p = parent
::validate( $value, $alldata );
41 if ( !is_array( $value ) ) {
45 # If all options are valid, array_intersect of the valid options
46 # and the provided options will return the provided options.
47 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
49 $validValues = array_intersect( $value, $validOptions );
50 if ( count( $validValues ) == count( $value ) ) {
53 return $this->msg( 'htmlform-select-badoption' );
57 public function getInputHTML( $value ) {
58 if ( isset( $this->mParams
['dropdown'] ) ) {
59 $this->mParent
->getOutput()->addModules( 'jquery.chosen' );
62 $value = HTMLFormField
::forceToStringRecursive( $value );
63 $html = $this->formatOptions( $this->getOptions(), $value );
68 public function formatOptions( $options, $value ) {
71 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
73 foreach ( $options as $label => $info ) {
74 if ( is_array( $info ) ) {
75 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
76 $html .= $this->formatOptions( $info, $value );
79 'id' => "{$this->mID}-$info",
82 if ( in_array( $info, $this->mParams
['disabled-options'], true ) ) {
83 $thisAttribs['disabled'] = 'disabled';
85 $checked = in_array( $info, $value, true );
87 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
89 $html .= ' ' . Html
::rawElement(
91 [ 'class' => 'mw-htmlform-flatlist-item' ],
100 protected function getOneCheckbox( $checked, $attribs, $label ) {
101 if ( $this->mParent
instanceof OOUIHTMLForm
) {
102 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
104 $elementFunc = [ Html
::class, $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
106 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
108 call_user_func( $elementFunc,
110 [ 'for' => $attribs['id'] ],
113 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
114 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
116 Html
::closeElement( 'div' );
123 * Get options and make them into arrays suitable for OOUI.
124 * @throws MWException
126 public function getOptionsOOUI() {
127 // Sections make this difficult. See getInputOOUI().
128 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
132 * Get the OOUI version of this field.
134 * Returns OOUI\CheckboxMultiselectInputWidget for fields that only have one section,
138 * @param string[] $value
139 * @return string|OOUI\CheckboxMultiselectInputWidget
140 * @suppress PhanParamSignatureMismatch
142 public function getInputOOUI( $value ) {
143 $this->mParent
->getOutput()->addModules( 'oojs-ui-widgets' );
145 $hasSections = false;
146 $optionsOouiSections = [];
147 $options = $this->getOptions();
148 // If the options are supposed to be split into sections, each section becomes a separate
149 // CheckboxMultiselectInputWidget.
150 foreach ( $options as $label => $section ) {
151 if ( is_array( $section ) ) {
152 $optionsOouiSections[ $label ] = Xml
::listDropDownOptionsOoui( $section );
153 unset( $options[$label] );
157 // If anything remains in the array, they are sectionless options. Put them in a separate widget
160 $optionsOouiSections = array_merge(
161 [ '' => Xml
::listDropDownOptionsOoui( $options ) ],
167 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
169 $attr['name'] = "{$this->mName}[]";
171 $attr['value'] = $value;
172 $attr['options'] = $optionsOoui;
174 foreach ( $attr['options'] as &$option ) {
175 $option['disabled'] = in_array( $option['data'], $this->mParams
['disabled-options'], true );
177 if ( $this->mOptionsLabelsNotFromMessage
) {
178 foreach ( $attr['options'] as &$option ) {
179 $option['label'] = new OOUI\
HtmlSnippet( $option['label'] );
183 $attr +
= OOUI\Element
::configFromHtmlAttributes(
184 $this->getAttributes( [ 'disabled', 'tabindex' ] )
187 if ( $this->mClass
!== '' ) {
188 $attr['classes'] = [ $this->mClass
];
191 $widget = new OOUI\
CheckboxMultiselectInputWidget( $attr );
192 if ( $sectionLabel ) {
193 $out[] = new OOUI\
FieldsetLayout( [
194 'items' => [ $widget ],
195 'label' => new OOUI\
HtmlSnippet( $sectionLabel ),
202 if ( !$hasSections ) {
203 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
204 // This allows it to be made infusable and later tweaked by JS code.
208 return implode( '', $out );
212 * @param WebRequest $request
214 * @return string|array
216 public function loadDataFromRequest( $request ) {
217 $fromRequest = $request->getArray( $this->mName
, [] );
218 // Fetch the value in either one of the two following case:
219 // - we have a valid submit attempt (form was just submitted)
220 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
221 if ( $this->isSubmitAttempt( $request ) ||
$fromRequest ) {
222 // Checkboxes are just not added to the request arrays if they're not checked,
223 // so it's perfectly possible for there not to be an entry at all
226 // That's ok, the user has not yet submitted the form, so show the defaults
227 return $this->getDefault();
231 public function getDefault() {
232 return $this->mDefault ??
[];
235 public function filterDataForSubmit( $data ) {
236 $data = HTMLFormField
::forceToStringRecursive( $data );
237 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
240 foreach ( $options as $opt ) {
241 $res["$opt"] = in_array( $opt, $data, true );
247 protected function needsLabel() {