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