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