Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLTextAreaField.php
blobb514af8c3f457920ee8f594ee7bd9277b4b5b9e3
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use InvalidArgumentException;
6 use MediaWiki\Html\Html;
7 use MediaWiki\HTMLForm\HTMLFormField;
8 use MediaWiki\MediaWikiServices;
11 * @stable to extend
13 class HTMLTextAreaField extends HTMLFormField {
14 protected const DEFAULT_COLS = 80;
15 protected const DEFAULT_ROWS = 25;
17 /** @var string */
18 protected $mPlaceholder = '';
19 /** @var bool */
20 protected $mUseEditFont = false;
22 /**
23 * @stable to call
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';
59 return null;
62 /**
63 * @inheritDoc
64 * @stable to override
66 public function getInputHTML( $value ) {
67 $classes = [];
69 $attribs = [
70 'id' => $this->mID,
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
86 $classes[] =
87 'mw-editfont-' .
88 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
89 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
91 if ( $this->mPlaceholder !== '' ) {
92 $attribs['placeholder'] = $this->mPlaceholder;
94 if ( $classes ) {
95 $attribs['class'] = $classes;
98 $allowedParams = [
99 'maxlength',
100 'minlength',
101 'tabindex',
102 'disabled',
103 'readonly',
104 'required',
105 'autofocus'
108 $attribs += $this->getAttributes( $allowedParams );
109 return Html::textarea( $this->mName, $value, $attribs );
113 * @inheritDoc
114 * @stable to override
116 public function getInputOOUI( $value ) {
117 $classes = [];
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
135 $classes[] =
136 'mw-editfont-' .
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;
147 $allowedParams = [
148 'maxlength',
149 'minlength',
150 'tabindex',
151 'disabled',
152 'readonly',
153 'required',
154 'autofocus',
157 $attribs += \OOUI\Element::configFromHtmlAttributes(
158 $this->getAttributes( $allowedParams )
161 return new \OOUI\MultilineTextInputWidget( [
162 'id' => $this->mID,
163 'name' => $this->mName,
164 'value' => $value,
165 'rows' => $this->getRows(),
166 ] + $attribs );
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
181 $textareaClasses[] =
182 'mw-editfont-' .
183 $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
184 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
187 $textareaAttribs = [
188 'id' => $this->mID,
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;
199 $allowedParams = [
200 'maxlength',
201 'minlength',
202 'tabindex',
203 'disabled',
204 'readonly',
205 'required',
206 'autofocus'
208 $textareaAttribs += $this->getAttributes( $allowedParams );
210 $textarea = Html::textarea( $this->mName, $value, $textareaAttribs );
212 $wrapperAttribs = [ 'class' => [ 'cdx-text-area' ] ];
213 if ( $hasErrors ) {
214 $wrapperAttribs['class'][] = 'cdx-text-area--status-error';
216 return Html::rawElement(
217 'div',
218 $wrapperAttribs,
219 $textarea
224 /** @deprecated class alias since 1.42 */
225 class_alias( HTMLTextAreaField::class, 'HTMLTextAreaField' );