MediaWiki REL1_39
HTMLTextAreaField.php
Go to the documentation of this file.
1<?php
2
3/*
4 * @stable to extend
5 */
6
8
10 protected const DEFAULT_COLS = 80;
11 protected const DEFAULT_ROWS = 25;
12
13 protected $mPlaceholder = '';
14 protected $mUseEditFont = false;
15
25 public function __construct( $params ) {
26 parent::__construct( $params );
27
28 if ( isset( $params['placeholder-message'] ) ) {
29 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
30 } elseif ( isset( $params['placeholder'] ) ) {
31 $this->mPlaceholder = $params['placeholder'];
32 }
33
34 if ( isset( $params['useeditfont'] ) ) {
35 $this->mUseEditFont = $params['useeditfont'];
36 }
37 }
38
39 public function getCols() {
40 return $this->mParams['cols'] ?? static::DEFAULT_COLS;
41 }
42
43 public function getRows() {
44 return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
45 }
46
47 public function getSpellCheck() {
48 $val = $this->mParams['spellcheck'] ?? null;
49 if ( is_bool( $val ) ) {
50 // "spellcheck" attribute literally requires "true" or "false" to work.
51 return $val ? 'true' : 'false';
52 }
53 return null;
54 }
55
60 public function getInputHTML( $value ) {
61 $classes = [];
62
63 $attribs = [
64 'id' => $this->mID,
65 'cols' => $this->getCols(),
66 'rows' => $this->getRows(),
67 'spellcheck' => $this->getSpellCheck(),
68 ] + $this->getTooltipAndAccessKey();
69
70 if ( $this->mClass !== '' ) {
71 array_push( $classes, $this->mClass );
72 }
73 if ( $this->mUseEditFont ) {
74 $userOptionsLookup = MediaWikiServices::getInstance()
75 ->getUserOptionsLookup();
76 // The following classes can be used here:
77 // * mw-editfont-monospace
78 // * mw-editfont-sans-serif
79 // * mw-editfont-serif
80 array_push(
81 $classes,
82 'mw-editfont-' . $userOptionsLookup->getOption(
83 $this->mParent->getUser(),
84 'editfont'
85 )
86 );
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 'tabindex',
98 'disabled',
99 'readonly',
100 'required',
101 'autofocus'
102 ];
103
104 $attribs += $this->getAttributes( $allowedParams );
105 return Html::textarea( $this->mName, $value, $attribs );
106 }
107
112 public function getInputOOUI( $value ) {
113 $classes = [];
114
115 if ( isset( $this->mParams['cols'] ) ) {
116 throw new Exception( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
117 }
118
119 $attribs = $this->getTooltipAndAccessKeyOOUI();
120
121 if ( $this->mClass !== '' ) {
122 array_push( $classes, $this->mClass );
123 }
124 if ( $this->mUseEditFont ) {
125 $userOptionsLookup = MediaWikiServices::getInstance()
126 ->getUserOptionsLookup();
127 // The following classes can be used here:
128 // * mw-editfont-monospace
129 // * mw-editfont-sans-serif
130 // * mw-editfont-serif
131 array_push(
132 $classes,
133 'mw-editfont-' . $userOptionsLookup->getOption(
134 $this->mParent->getUser(),
135 'editfont'
136 )
137 );
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 '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}
The parent class to generate form fields.
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.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
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 "...
Service locator for MediaWiki core services.