MediaWiki 1.41.2
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
4use OOUI\Widget;
5
17 protected $mPlaceholder = '';
18
20 protected $autocomplete;
21
33 public function __construct( $params ) {
34 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
35 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
36 }
37
38 parent::__construct( $params );
39
40 if ( isset( $params['placeholder-message'] ) ) {
41 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
42 } elseif ( isset( $params['placeholder'] ) ) {
43 $this->mPlaceholder = $params['placeholder'];
44 }
45 }
46
51 public function getSize() {
52 return $this->mParams['size'] ?? 45;
53 }
54
55 public function getSpellCheck() {
56 $val = $this->mParams['spellcheck'] ?? null;
57 if ( is_bool( $val ) ) {
58 // "spellcheck" attribute literally requires "true" or "false" to work.
59 return $val ? 'true' : 'false';
60 }
61 return null;
62 }
63
64 public function isPersistent() {
65 if ( isset( $this->mParams['persistent'] ) ) {
66 return $this->mParams['persistent'];
67 }
68 // don't put passwords into the HTML body, they could get cached or otherwise leaked
69 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
70 }
71
76 public function getInputHTML( $value ) {
77 if ( !$this->isPersistent() ) {
78 $value = '';
79 }
80
81 $attribs = [
82 'id' => $this->mID,
83 'name' => $this->mName,
84 'size' => $this->getSize(),
85 'value' => $value,
86 'dir' => $this->mDir,
87 'spellcheck' => $this->getSpellCheck(),
88 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
89
90 if ( $this->mClass !== '' ) {
91 $attribs['class'] = $this->mClass;
92 }
93 if ( $this->mPlaceholder !== '' ) {
94 $attribs['placeholder'] = $this->mPlaceholder;
95 }
96
97 # @todo Enforce pattern, step, required, readonly on the server side as
98 # well
99 $allowedParams = [
100 'type',
101 'min',
102 'max',
103 'step',
104 'title',
105 'maxlength',
106 'minlength',
107 'tabindex',
108 'disabled',
109 'required',
110 'autofocus',
111 'readonly',
112 'autocomplete',
113 // Only used in HTML mode:
114 'pattern',
115 'list',
116 ];
117
118 $attribs += $this->getAttributes( $allowedParams );
119
120 # Extract 'type'
121 $type = $this->getType( $attribs );
122 $isCodexForm = $this->mParent && $this->mParent instanceof CodexHTMLForm;
123 if ( $isCodexForm ) {
124 $class = $attribs['class'] ?? [];
125 if ( is_string( $class ) ) {
126 $attribs['class'] .= ' cdx-text-input__input';
127 } else {
128 $class[] = 'cdx-text-input__input';
129 $attribs['class'] = $class;
130 }
131 }
132 $inputHtml = Html::input( $this->mName, $value, $type, $attribs );
133 return $isCodexForm
134 ? Html::rawElement( 'div', [ 'class' => 'cdx-text-input' ], $inputHtml )
135 : $inputHtml;
136 }
137
138 protected function getType( &$attribs ) {
139 $type = $attribs['type'] ?? 'text';
140 unset( $attribs['type'] );
141
142 # Implement tiny differences between some field variants
143 # here, rather than creating a new class for each one which
144 # is essentially just a clone of this one.
145 if ( isset( $this->mParams['type'] ) ) {
146 switch ( $this->mParams['type'] ) {
147 case 'int':
148 $type = 'number';
149 $attribs['step'] = 1;
150 break;
151 case 'float':
152 $type = 'number';
153 $attribs['step'] = 'any';
154 break;
155 # Pass through
156 case 'email':
157 case 'password':
158 case 'url':
159 $type = $this->mParams['type'];
160 break;
161 case 'textwithbutton':
162 $type = $this->mParams['inputtype'] ?? 'text';
163 break;
164 }
165 }
166
167 return $type;
168 }
169
174 public function getInputOOUI( $value ) {
175 if ( !$this->isPersistent() ) {
176 $value = '';
177 }
178
179 $attribs = $this->getTooltipAndAccessKeyOOUI();
180
181 if ( $this->mClass !== '' ) {
182 $attribs['classes'] = [ $this->mClass ];
183 }
184 if ( $this->mPlaceholder !== '' ) {
185 $attribs['placeholder'] = $this->mPlaceholder;
186 }
187
188 # @todo Enforce pattern, step, required, readonly on the server side as
189 # well
190 $allowedParams = [
191 'type',
192 'min',
193 'max',
194 'step',
195 'title',
196 'maxlength',
197 'minlength',
198 'tabindex',
199 'disabled',
200 'required',
201 'autofocus',
202 'readonly',
203 'autocomplete',
204 // Only used in OOUI mode:
205 'autosize',
206 'flags',
207 'indicator',
208 ];
209
210 $attribs += OOUI\Element::configFromHtmlAttributes(
211 $this->getAttributes( $allowedParams )
212 );
213
214 $type = $this->getType( $attribs );
215 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
216 $attribs['step'] = null;
217 }
218
219 return $this->getInputWidget( [
220 'id' => $this->mID,
221 'name' => $this->mName,
222 'value' => $value,
223 'type' => $type,
224 'dir' => $this->mDir,
225 ] + $attribs );
226 }
227
235 protected function getInputWidget( $params ) {
236 return new OOUI\TextInputWidget( $params );
237 }
238
245 protected function getDataAttribs() {
246 return [];
247 }
248}
Codex based HTML form.
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.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57