MediaWiki master
HTMLTextAreaField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
9
14 protected const DEFAULT_COLS = 80;
15 protected const DEFAULT_ROWS = 25;
16
18 protected $mPlaceholder = '';
20 protected $mUseEditFont = false;
21
31 public function __construct( $params ) {
32 parent::__construct( $params );
33
34 if ( isset( $params['placeholder-message'] ) ) {
35 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
36 } elseif ( isset( $params['placeholder'] ) ) {
37 $this->mPlaceholder = $params['placeholder'];
38 }
39
40 if ( isset( $params['useeditfont'] ) ) {
41 $this->mUseEditFont = $params['useeditfont'];
42 }
43 }
44
48 public function getCols() {
49 return $this->mParams['cols'] ?? static::DEFAULT_COLS;
50 }
51
55 public function getRows() {
56 return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
57 }
58
62 public function getSpellCheck() {
63 $val = $this->mParams['spellcheck'] ?? null;
64 if ( is_bool( $val ) ) {
65 // "spellcheck" attribute literally requires "true" or "false" to work.
66 return $val ? 'true' : 'false';
67 }
68 return null;
69 }
70
75 public function getInputHTML( $value ) {
76 $classes = [];
77
78 $attribs = [
79 'id' => $this->mID,
80 'cols' => $this->getCols(),
81 'rows' => $this->getRows(),
82 'spellcheck' => $this->getSpellCheck(),
83 ] + $this->getTooltipAndAccessKey();
84
85 if ( $this->mClass !== '' ) {
86 $classes[] = $this->mClass;
87 }
88 if ( $this->mUseEditFont ) {
89 $userOptionsLookup = MediaWikiServices::getInstance()
90 ->getUserOptionsLookup();
91 // The following classes can be used here:
92 // * mw-editfont-monospace
93 // * mw-editfont-sans-serif
94 // * mw-editfont-serif
95 $classes[] =
96 'mw-editfont-' .
97 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
98 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
99 }
100 if ( $this->mPlaceholder !== '' ) {
101 $attribs['placeholder'] = $this->mPlaceholder;
102 }
103 if ( $classes ) {
104 $attribs['class'] = $classes;
105 }
106
107 $allowedParams = [
108 'maxlength',
109 'minlength',
110 'tabindex',
111 'disabled',
112 'readonly',
113 'required',
114 'autofocus'
115 ];
116
117 $attribs += $this->getAttributes( $allowedParams );
118 return Html::textarea( $this->mName, $value, $attribs );
119 }
120
125 public function getInputOOUI( $value ) {
126 $classes = [];
127
128 if ( isset( $this->mParams['cols'] ) ) {
129 throw new InvalidArgumentException( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
130 }
131
132 $attribs = $this->getTooltipAndAccessKeyOOUI();
133
134 if ( $this->mClass !== '' ) {
135 $classes[] = $this->mClass;
136 }
137 if ( $this->mUseEditFont ) {
138 $userOptionsLookup = MediaWikiServices::getInstance()
139 ->getUserOptionsLookup();
140 // The following classes can be used here:
141 // * mw-editfont-monospace
142 // * mw-editfont-sans-serif
143 // * mw-editfont-serif
144 $classes[] =
145 'mw-editfont-' .
146 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
147 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
148 }
149 if ( $this->mPlaceholder !== '' ) {
150 $attribs['placeholder'] = $this->mPlaceholder;
151 }
152 if ( count( $classes ) ) {
153 $attribs['classes'] = $classes;
154 }
155
156 $allowedParams = [
157 'maxlength',
158 'minlength',
159 'tabindex',
160 'disabled',
161 'readonly',
162 'required',
163 'autofocus',
164 ];
165
166 $attribs += \OOUI\Element::configFromHtmlAttributes(
167 $this->getAttributes( $allowedParams )
168 );
169
170 return new \OOUI\MultilineTextInputWidget( [
171 'id' => $this->mID,
172 'name' => $this->mName,
173 'value' => $value,
174 'rows' => $this->getRows(),
175 ] + $attribs );
176 }
177
179 public function getInputCodex( $value, $hasErrors ) {
180 $textareaClasses = [ 'cdx-text-area__textarea' ];
181 if ( $this->mClass !== '' ) {
182 $textareaClasses[] = $this->mClass;
183 }
184 if ( $this->mUseEditFont ) {
185 $userOptionsLookup = MediaWikiServices::getInstance()
186 ->getUserOptionsLookup();
187 // The following classes can be used here:
188 // * mw-editfont-monospace
189 // * mw-editfont-sans-serif
190 // * mw-editfont-serif
191 $textareaClasses[] =
192 'mw-editfont-' .
193 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
194 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
195 }
196
197 $textareaAttribs = [
198 'id' => $this->mID,
199 'cols' => $this->getCols(),
200 'rows' => $this->getRows(),
201 'spellcheck' => $this->getSpellCheck(),
202 'class' => $textareaClasses
203 ] + $this->getTooltipAndAccessKey();
204
205 if ( $this->mPlaceholder !== '' ) {
206 $textareaAttribs['placeholder'] = $this->mPlaceholder;
207 }
208
209 $allowedParams = [
210 'maxlength',
211 'minlength',
212 'tabindex',
213 'disabled',
214 'readonly',
215 'required',
216 'autofocus'
217 ];
218 $textareaAttribs += $this->getAttributes( $allowedParams );
219
220 $textarea = Html::textarea( $this->mName, $value, $textareaAttribs );
221
222 $wrapperAttribs = [ 'class' => [ 'cdx-text-area' ] ];
223 if ( $hasErrors ) {
224 $wrapperAttribs['class'][] = 'cdx-text-area--status-error';
225 }
226 return Html::rawElement(
227 'div',
228 $wrapperAttribs,
229 $textarea
230 );
231 }
232}
233
235class_alias( HTMLTextAreaField::class, 'HTMLTextAreaField' );
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
The parent class to generate form fields.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
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.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
getOption(UserIdentity $user, string $oname, $defaultOverride=null, bool $ignoreHidden=false, int $queryFlags=IDBAccessObject::READ_NORMAL)
Get the user's current setting for a given option.