3 namespace MediaWiki\HTMLForm\Field
;
5 use InvalidArgumentException
;
6 use MediaWiki\Html\Html
;
7 use MediaWiki\HTMLForm\HTMLFormField
;
8 use MediaWiki\MediaWikiServices
;
13 class HTMLTextAreaField
extends HTMLFormField
{
14 protected const DEFAULT_COLS
= 80;
15 protected const DEFAULT_ROWS
= 25;
18 protected $mPlaceholder = '';
20 protected $mUseEditFont = false;
25 * @param array $params
26 * - cols, rows: textarea size
27 * - placeholder/placeholder-message: set HTML placeholder attribute
28 * - spellcheck: set HTML spellcheck attribute
29 * - useeditfont: add CSS classes to use the same font as the wikitext editor
31 public function __construct( $params ) {
32 parent
::__construct( $params );
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'];
40 if ( isset( $params['useeditfont'] ) ) {
41 $this->mUseEditFont
= $params['useeditfont'];
45 public function getCols() {
46 return $this->mParams
['cols'] ??
static::DEFAULT_COLS
;
49 public function getRows() {
50 return $this->mParams
['rows'] ??
static::DEFAULT_ROWS
;
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';
66 public function getInputHTML( $value ) {
71 'cols' => $this->getCols(),
72 'rows' => $this->getRows(),
73 'spellcheck' => $this->getSpellCheck(),
74 ] +
$this->getTooltipAndAccessKey();
76 if ( $this->mClass
!== '' ) {
77 $classes[] = $this->mClass
;
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
88 $userOptionsLookup->getOption( $this->mParent
->getUser(), 'editfont' );
89 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
91 if ( $this->mPlaceholder
!== '' ) {
92 $attribs['placeholder'] = $this->mPlaceholder
;
95 $attribs['class'] = $classes;
108 $attribs +
= $this->getAttributes( $allowedParams );
109 return Html
::textarea( $this->mName
, $value, $attribs );
114 * @stable to override
116 public function getInputOOUI( $value ) {
119 if ( isset( $this->mParams
['cols'] ) ) {
120 throw new InvalidArgumentException( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
123 $attribs = $this->getTooltipAndAccessKeyOOUI();
125 if ( $this->mClass
!== '' ) {
126 $classes[] = $this->mClass
;
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
137 $userOptionsLookup->getOption( $this->mParent
->getUser(), 'editfont' );
138 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
140 if ( $this->mPlaceholder
!== '' ) {
141 $attribs['placeholder'] = $this->mPlaceholder
;
143 if ( count( $classes ) ) {
144 $attribs['classes'] = $classes;
157 $attribs +
= \OOUI\Element
::configFromHtmlAttributes(
158 $this->getAttributes( $allowedParams )
161 return new \OOUI\
MultilineTextInputWidget( [
163 'name' => $this->mName
,
165 'rows' => $this->getRows(),
169 public function getInputCodex( $value, $hasErrors ) {
170 $textareaClasses = [ 'cdx-text-area__textarea' ];
171 if ( $this->mClass
!== '' ) {
172 $textareaClasses[] = $this->mClass
;
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
183 $userOptionsLookup->getOption( $this->mParent
->getUser(), 'editfont' );
184 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
189 'cols' => $this->getCols(),
190 'rows' => $this->getRows(),
191 'spellcheck' => $this->getSpellCheck(),
192 'class' => $textareaClasses
193 ] +
$this->getTooltipAndAccessKey();
195 if ( $this->mPlaceholder
!== '' ) {
196 $textareaAttribs['placeholder'] = $this->mPlaceholder
;
208 $textareaAttribs +
= $this->getAttributes( $allowedParams );
210 $textarea = Html
::textarea( $this->mName
, $value, $textareaAttribs );
212 $wrapperAttribs = [ 'class' => [ 'cdx-text-area' ] ];
214 $wrapperAttribs['class'][] = 'cdx-text-area--status-error';
216 return Html
::rawElement(
224 /** @deprecated class alias since 1.42 */
225 class_alias( HTMLTextAreaField
::class, 'HTMLTextAreaField' );