MediaWiki master
HTMLTextAreaField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
9
10/*
11 * @stable to extend
12 */
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
45 public function getCols() {
46 return $this->mParams['cols'] ?? static::DEFAULT_COLS;
47 }
48
49 public function getRows() {
50 return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
51 }
52
53 public function getSpellCheck() {
54 $val = $this->mParams['spellcheck'] ?? null;
55 if ( is_bool( $val ) ) {
56 // "spellcheck" attribute literally requires "true" or "false" to work.
57 return $val ? 'true' : 'false';
58 }
59 return null;
60 }
61
66 public function getInputHTML( $value ) {
67 $classes = [];
68
69 $attribs = [
70 'id' => $this->mID,
71 'cols' => $this->getCols(),
72 'rows' => $this->getRows(),
73 'spellcheck' => $this->getSpellCheck(),
74 ] + $this->getTooltipAndAccessKey();
75
76 if ( $this->mClass !== '' ) {
77 $classes[] = $this->mClass;
78 }
79 if ( $this->mUseEditFont ) {
80 $userOptionsLookup = MediaWikiServices::getInstance()
81 ->getUserOptionsLookup();
82 // The following classes can be used here:
83 // * mw-editfont-monospace
84 // * mw-editfont-sans-serif
85 // * mw-editfont-serif
86 $classes[] =
87 'mw-editfont-' .
88 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
89 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
90 }
91 if ( $this->mPlaceholder !== '' ) {
92 $attribs['placeholder'] = $this->mPlaceholder;
93 }
94 if ( $classes ) {
95 $attribs['class'] = $classes;
96 }
97
98 $allowedParams = [
99 'maxlength',
100 'minlength',
101 'tabindex',
102 'disabled',
103 'readonly',
104 'required',
105 'autofocus'
106 ];
107
108 $attribs += $this->getAttributes( $allowedParams );
109 return Html::textarea( $this->mName, $value, $attribs );
110 }
111
116 public function getInputOOUI( $value ) {
117 $classes = [];
118
119 if ( isset( $this->mParams['cols'] ) ) {
120 throw new InvalidArgumentException( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
121 }
122
123 $attribs = $this->getTooltipAndAccessKeyOOUI();
124
125 if ( $this->mClass !== '' ) {
126 $classes[] = $this->mClass;
127 }
128 if ( $this->mUseEditFont ) {
129 $userOptionsLookup = MediaWikiServices::getInstance()
130 ->getUserOptionsLookup();
131 // The following classes can be used here:
132 // * mw-editfont-monospace
133 // * mw-editfont-sans-serif
134 // * mw-editfont-serif
135 $classes[] =
136 'mw-editfont-' .
137 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
138 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
139 }
140 if ( $this->mPlaceholder !== '' ) {
141 $attribs['placeholder'] = $this->mPlaceholder;
142 }
143 if ( count( $classes ) ) {
144 $attribs['classes'] = $classes;
145 }
146
147 $allowedParams = [
148 'maxlength',
149 'minlength',
150 'tabindex',
151 'disabled',
152 'readonly',
153 'required',
154 'autofocus',
155 ];
156
157 $attribs += \OOUI\Element::configFromHtmlAttributes(
158 $this->getAttributes( $allowedParams )
159 );
160
161 return new \OOUI\MultilineTextInputWidget( [
162 'id' => $this->mID,
163 'name' => $this->mName,
164 'value' => $value,
165 'rows' => $this->getRows(),
166 ] + $attribs );
167 }
168
169 public function getInputCodex( $value, $hasErrors ) {
170 $textareaClasses = [ 'cdx-text-area__textarea' ];
171 if ( $this->mClass !== '' ) {
172 $textareaClasses[] = $this->mClass;
173 }
174 if ( $this->mUseEditFont ) {
175 $userOptionsLookup = MediaWikiServices::getInstance()
176 ->getUserOptionsLookup();
177 // The following classes can be used here:
178 // * mw-editfont-monospace
179 // * mw-editfont-sans-serif
180 // * mw-editfont-serif
181 $textareaClasses[] =
182 'mw-editfont-' .
183 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
184 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
185 }
186
187 $textareaAttribs = [
188 'id' => $this->mID,
189 'cols' => $this->getCols(),
190 'rows' => $this->getRows(),
191 'spellcheck' => $this->getSpellCheck(),
192 'class' => $textareaClasses
193 ] + $this->getTooltipAndAccessKey();
194
195 if ( $this->mPlaceholder !== '' ) {
196 $textareaAttribs['placeholder'] = $this->mPlaceholder;
197 }
198
199 $allowedParams = [
200 'maxlength',
201 'minlength',
202 'tabindex',
203 'disabled',
204 'readonly',
205 'required',
206 'autofocus'
207 ];
208 $textareaAttribs += $this->getAttributes( $allowedParams );
209
210 $textarea = Html::textarea( $this->mName, $value, $textareaAttribs );
211
212 $wrapperAttribs = [ 'class' => [ 'cdx-text-area' ] ];
213 if ( $hasErrors ) {
214 $wrapperAttribs['class'][] = 'cdx-text-area--status-error';
215 }
216 return Html::rawElement(
217 'div',
218 $wrapperAttribs,
219 $textarea
220 );
221 }
222}
223
225class_alias( HTMLTextAreaField::class, 'HTMLTextAreaField' );
array $params
The job parameters.
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.
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:56
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.