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