MediaWiki REL1_35
HTMLSelectAndOtherField.php
Go to the documentation of this file.
1<?php
2
18
19 /*
20 * @stable to call
21 */
22 public function __construct( $params ) {
23 if ( array_key_exists( 'other', $params ) ) {
24 // Do nothing
25 } elseif ( array_key_exists( 'other-message', $params ) ) {
26 $params['other'] = $this->getMessage( $params['other-message'] )->plain();
27 } else {
28 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
29 }
30
31 parent::__construct( $params );
32
33 if ( $this->getOptions() === null ) {
34 // Sulk
35 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
36 }
37 if ( !in_array( 'other', $this->mOptions, true ) ) {
38 // Have 'other' always as first element
39 $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
40 }
41 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
42 }
43
44 public function getInputHTML( $value ) {
45 $select = parent::getInputHTML( $value[1] );
46
47 $textAttribs = [
48 'id' => $this->mID . '-other',
49 'size' => $this->getSize(),
50 'class' => [ 'mw-htmlform-select-and-other-field' ],
51 'data-id-select' => $this->mID,
52 ];
53
54 if ( $this->mClass !== '' ) {
55 $textAttribs['class'][] = $this->mClass;
56 }
57
58 if ( isset( $this->mParams['maxlength-unit'] ) ) {
59 $textAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
60 }
61
62 $allowedParams = [
63 'required',
64 'autofocus',
65 'multiple',
66 'disabled',
67 'tabindex',
68 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
69 'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
70 ];
71
72 $textAttribs += $this->getAttributes( $allowedParams );
73
74 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
75
76 return "$select<br />\n$textbox";
77 }
78
79 protected function getOOUIModules() {
80 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
81 }
82
83 public function getInputOOUI( $value ) {
84 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
85
86 # TextInput
87 $textAttribs = [
88 'name' => $this->mName . '-other',
89 'value' => $value[2],
90 ];
91
92 $allowedParams = [
93 'required',
94 'autofocus',
95 'multiple',
96 'disabled',
97 'tabindex',
98 'maxlength',
99 ];
100
101 $textAttribs += OOUI\Element::configFromHtmlAttributes(
102 $this->getAttributes( $allowedParams )
103 );
104
105 if ( $this->mClass !== '' ) {
106 $textAttribs['classes'] = [ $this->mClass ];
107 }
108
109 # DropdownInput
110 $dropdownInputAttribs = [
111 'name' => $this->mName,
112 'id' => $this->mID . '-select',
113 'options' => $this->getOptionsOOUI(),
114 'value' => $value[1],
115 ];
116
117 $allowedParams = [
118 'tabindex',
119 'disabled',
120 ];
121
122 $dropdownInputAttribs += OOUI\Element::configFromHtmlAttributes(
123 $this->getAttributes( $allowedParams )
124 );
125
126 if ( $this->mClass !== '' ) {
127 $dropdownInputAttribs['classes'] = [ $this->mClass ];
128 }
129
130 $disabled = false;
131 if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
132 $disabled = true;
133 }
134
135 return $this->getInputWidget( [
136 'id' => $this->mID,
137 'disabled' => $disabled,
138 'textinput' => $textAttribs,
139 'dropdowninput' => $dropdownInputAttribs,
140 'or' => false,
141 'required' => $this->mParams[ 'required' ] ?? false,
142 'classes' => [ 'mw-htmlform-select-and-other-field' ],
143 'data' => [
144 'maxlengthUnit' => $this->mParams['maxlength-unit'] ?? 'bytes'
145 ],
146 ] );
147 }
148
153 public function getInputWidget( $params ) {
154 return new MediaWiki\Widget\SelectWithInputWidget( $params );
155 }
156
160 public function getDefault() {
161 $default = parent::getDefault();
162
163 // Default values of empty form
164 $final = '';
165 $list = 'other';
166 $text = '';
167
168 if ( $default !== null ) {
169 $final = $default;
170 // Assume the default is a text value, with the 'other' option selected.
171 // Then check if that assumption is correct, and update $list and $text if not.
172 $text = $final;
173 foreach ( $this->mFlatOptions as $option ) {
174 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
175 if ( strpos( $final, $match ) === 0 ) {
176 $list = $option;
177 $text = substr( $final, strlen( $match ) );
178 break;
179 }
180 }
181 }
182
183 return [ $final, $list, $text ];
184 }
185
191 public function loadDataFromRequest( $request ) {
192 if ( $request->getCheck( $this->mName ) ) {
193 $list = $request->getText( $this->mName );
194 $text = $request->getText( $this->mName . '-other' );
195
196 // Should be built the same as in mediawiki.htmlform.js
197 if ( $list == 'other' ) {
198 $final = $text;
199 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
200 # User has spoofed the select form to give an option which wasn't
201 # in the original offer. Sulk...
202 $final = $text;
203 } elseif ( $text == '' ) {
204 $final = $list;
205 } else {
206 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
207 }
208 return [ $final, $list, $text ];
209 }
210 return $this->getDefault();
211 }
212
213 public function getSize() {
214 return $this->mParams['size'] ?? 45;
215 }
216
217 public function validate( $value, $alldata ) {
218 # HTMLSelectField forces $value to be one of the options in the select
219 # field, which is not useful here. But we do want the validation further up
220 # the chain
221 $p = parent::validate( $value[1], $alldata );
222
223 if ( $p !== true ) {
224 return $p;
225 }
226
227 if ( isset( $this->mParams['required'] )
228 && $this->mParams['required'] !== false
229 && $value[0] === ''
230 ) {
231 return $this->msg( 'htmlform-required' );
232 }
233
234 return true;
235 }
236}
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
array bool null $mOptions
msg( $key,... $params)
Get a translated interface message.
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Double field with a dropdown list constructed from a system message in the format.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
__construct( $params)
Initialise the object.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getDefault()
Stable to override.mixed
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getInputWidget( $params)
Stable to override
A select dropdown field.
MediaWiki exception.