MediaWiki REL1_37
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
3use OOUI\Widget;
4
16 protected $mPlaceholder = '';
17
19 protected $autocomplete;
20
32 public function __construct( $params ) {
33 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
34 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
35 }
36
37 parent::__construct( $params );
38
39 if ( isset( $params['placeholder-message'] ) ) {
40 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
41 } elseif ( isset( $params['placeholder'] ) ) {
42 $this->mPlaceholder = $params['placeholder'];
43 }
44 }
45
50 public function getSize() {
51 return $this->mParams['size'] ?? 45;
52 }
53
54 public function getSpellCheck() {
55 $val = $this->mParams['spellcheck'] ?? null;
56 if ( is_bool( $val ) ) {
57 // "spellcheck" attribute literally requires "true" or "false" to work.
58 return $val ? 'true' : 'false';
59 }
60 return null;
61 }
62
63 public function isPersistent() {
64 if ( isset( $this->mParams['persistent'] ) ) {
65 return $this->mParams['persistent'];
66 }
67 // don't put passwords into the HTML body, they could get cached or otherwise leaked
68 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
69 }
70
75 public function getInputHTML( $value ) {
76 if ( !$this->isPersistent() ) {
77 $value = '';
78 }
79
80 $attribs = [
81 'id' => $this->mID,
82 'name' => $this->mName,
83 'size' => $this->getSize(),
84 'value' => $value,
85 'dir' => $this->mDir,
86 'spellcheck' => $this->getSpellCheck(),
87 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
88
89 if ( $this->mClass !== '' ) {
90 $attribs['class'] = $this->mClass;
91 }
92 if ( $this->mPlaceholder !== '' ) {
93 $attribs['placeholder'] = $this->mPlaceholder;
94 }
95
96 # @todo Enforce pattern, step, required, readonly on the server side as
97 # well
98 $allowedParams = [
99 'type',
100 'min',
101 'max',
102 'step',
103 'title',
104 'maxlength',
105 'tabindex',
106 'disabled',
107 'required',
108 'autofocus',
109 'readonly',
110 'autocomplete',
111 // Only used in HTML mode:
112 'pattern',
113 'list',
114 ];
115
116 $attribs += $this->getAttributes( $allowedParams );
117
118 # Extract 'type'
119 $type = $this->getType( $attribs );
120 return Html::input( $this->mName, $value, $type, $attribs );
121 }
122
123 protected function getType( &$attribs ) {
124 $type = $attribs['type'] ?? 'text';
125 unset( $attribs['type'] );
126
127 # Implement tiny differences between some field variants
128 # here, rather than creating a new class for each one which
129 # is essentially just a clone of this one.
130 if ( isset( $this->mParams['type'] ) ) {
131 switch ( $this->mParams['type'] ) {
132 case 'int':
133 $type = 'number';
134 $attribs['step'] = 1;
135 break;
136 case 'float':
137 $type = 'number';
138 $attribs['step'] = 'any';
139 break;
140 # Pass through
141 case 'email':
142 case 'password':
143 case 'url':
144 $type = $this->mParams['type'];
145 break;
146 case 'textwithbutton':
147 $type = $this->mParams['inputtype'] ?? 'text';
148 break;
149 }
150 }
151
152 return $type;
153 }
154
159 public function getInputOOUI( $value ) {
160 if ( !$this->isPersistent() ) {
161 $value = '';
162 }
163
164 $attribs = $this->getTooltipAndAccessKeyOOUI();
165
166 if ( $this->mClass !== '' ) {
167 $attribs['classes'] = [ $this->mClass ];
168 }
169 if ( $this->mPlaceholder !== '' ) {
170 $attribs['placeholder'] = $this->mPlaceholder;
171 }
172
173 # @todo Enforce pattern, step, required, readonly on the server side as
174 # well
175 $allowedParams = [
176 'type',
177 'min',
178 'max',
179 'step',
180 'title',
181 'maxlength',
182 'tabindex',
183 'disabled',
184 'required',
185 'autofocus',
186 'readonly',
187 'autocomplete',
188 // Only used in OOUI mode:
189 'autosize',
190 'flags',
191 'indicator',
192 ];
193
194 $attribs += OOUI\Element::configFromHtmlAttributes(
195 $this->getAttributes( $allowedParams )
196 );
197
198 $type = $this->getType( $attribs );
199 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
200 $attribs['step'] = null;
201 }
202
203 return $this->getInputWidget( [
204 'id' => $this->mID,
205 'name' => $this->mName,
206 'value' => $value,
207 'type' => $type,
208 'dir' => $this->mDir,
209 ] + $attribs );
210 }
211
219 protected function getInputWidget( $params ) {
220 return new OOUI\TextInputWidget( $params );
221 }
222
229 protected function getDataAttribs() {
230 return [];
231 }
232}
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
getAttributes(array $list)
Returns the given attributes from the parameters.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
<input> field.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputWidget( $params)
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getType(&$attribs)
__construct( $params)
getDataAttribs()
Returns an array of data-* attributes to add to the field.
bool $autocomplete
HTML autocomplete attribute.