MediaWiki REL1_35
HTMLSelectOrOtherField.php
Go to the documentation of this file.
1<?php
2
12
13 /*
14 * @stable to call
15 */
16 public function __construct( $params ) {
17 parent::__construct( $params );
18 $this->getOptions();
19 if ( !in_array( 'other', $this->mOptions, true ) ) {
20 $msg =
21 $params['other'] ?? wfMessage( 'htmlform-selectorother-other' )->text();
22 // Have 'other' always as first element
23 $this->mOptions = [ $msg => 'other' ] + $this->mOptions;
24 }
25 }
26
27 public function getInputHTML( $value ) {
28 $valInSelect = false;
29
30 if ( $value !== false ) {
31 $value = strval( $value );
32 $valInSelect = in_array(
33 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
34 );
35 }
36
37 $selected = $valInSelect ? $value : 'other';
38
39 $select = new XmlSelect( $this->mName, $this->mID, $selected );
40 $select->addOptions( $this->getOptions() );
41
42 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
43
44 $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
45
46 if ( !empty( $this->mParams['disabled'] ) ) {
47 $select->setAttribute( 'disabled', 'disabled' );
48 $tbAttribs['disabled'] = 'disabled';
49 }
50
51 if ( isset( $this->mParams['tabindex'] ) ) {
52 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
53 $tbAttribs['tabindex'] = $this->mParams['tabindex'];
54 }
55
56 $select = $select->getHTML();
57
58 if ( isset( $this->mParams['maxlength'] ) ) {
59 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
60 }
61
62 if ( $this->mClass !== '' ) {
63 $tbAttribs['class'] = $this->mClass;
64 }
65
66 $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
67
68 return "$select<br />\n$textbox";
69 }
70
71 protected function shouldInfuseOOUI() {
72 return true;
73 }
74
75 protected function getOOUIModules() {
76 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
77 }
78
79 public function getInputOOUI( $value ) {
80 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
81
82 $valInSelect = false;
83 if ( $value !== false ) {
84 $value = strval( $value );
85 $valInSelect = in_array(
86 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
87 );
88 }
89
90 # DropdownInput
91 $dropdownAttribs = [
92 'name' => $this->mName,
93 'options' => $this->getOptionsOOUI(),
94 'value' => $valInSelect ? $value : 'other',
95 'class' => [ 'mw-htmlform-select-or-other' ],
96 ];
97
98 $allowedParams = [
99 'disabled',
100 'tabindex',
101 ];
102
103 $dropdownAttribs += OOUI\Element::configFromHtmlAttributes(
104 $this->getAttributes( $allowedParams )
105 );
106
107 # TextInput
108 $textAttribs = [
109 'name' => $this->mName . '-other',
110 'size' => $this->getSize(),
111 'value' => $valInSelect ? '' : $value,
112 ];
113
114 $allowedParams = [
115 'required',
116 'autofocus',
117 'multiple',
118 'disabled',
119 'tabindex',
120 'maxlength',
121 ];
122
123 $textAttribs += OOUI\Element::configFromHtmlAttributes(
124 $this->getAttributes( $allowedParams )
125 );
126
127 if ( $this->mClass !== '' ) {
128 $textAttribs['classes'] = [ $this->mClass ];
129 }
130 if ( $this->mPlaceholder !== '' ) {
131 $textAttribs['placeholder'] = $this->mPlaceholder;
132 }
133
134 $disabled = false;
135 if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
136 $disabled = true;
137 }
138
139 return $this->getInputWidget( [
140 'id' => $this->mID,
141 'disabled' => $disabled,
142 'textinput' => $textAttribs,
143 'dropdowninput' => $dropdownAttribs,
144 'required' => $this->mParams[ 'required' ] ?? false,
145 'or' => true,
146 ] );
147 }
148
149 public function getInputWidget( $params ) {
150 return new MediaWiki\Widget\SelectWithInputWidget( $params );
151 }
152
158 public function loadDataFromRequest( $request ) {
159 if ( $request->getCheck( $this->mName ) ) {
160 $val = $request->getText( $this->mName );
161
162 if ( $val === 'other' ) {
163 $val = $request->getText( $this->mName . '-other' );
164 }
165
166 return $val;
167 } else {
168 return $this->getDefault();
169 }
170 }
171}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
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
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
getDefault()
Stable to override.
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Select dropdown field, with an additional "other" textbox.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
shouldInfuseOOUI()
Whether the field should be automatically infused.
getInputWidget( $params)
Stable to override.
__construct( $params)
Stable to call.
<input> field.
getSize()
Stable to override.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:26