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