MediaWiki  master
HTMLTextAreaField.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * @stable to extend
5  */
6 
9 
11  protected const DEFAULT_COLS = 80;
12  protected const DEFAULT_ROWS = 25;
13 
14  protected $mPlaceholder = '';
15  protected $mUseEditFont = false;
16 
26  public function __construct( $params ) {
27  parent::__construct( $params );
28 
29  if ( isset( $params['placeholder-message'] ) ) {
30  $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
31  } elseif ( isset( $params['placeholder'] ) ) {
32  $this->mPlaceholder = $params['placeholder'];
33  }
34 
35  if ( isset( $params['useeditfont'] ) ) {
36  $this->mUseEditFont = $params['useeditfont'];
37  }
38  }
39 
40  public function getCols() {
41  return $this->mParams['cols'] ?? static::DEFAULT_COLS;
42  }
43 
44  public function getRows() {
45  return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
46  }
47 
48  public function getSpellCheck() {
49  $val = $this->mParams['spellcheck'] ?? null;
50  if ( is_bool( $val ) ) {
51  // "spellcheck" attribute literally requires "true" or "false" to work.
52  return $val ? 'true' : 'false';
53  }
54  return null;
55  }
56 
61  public function getInputHTML( $value ) {
62  $classes = [];
63 
64  $attribs = [
65  'id' => $this->mID,
66  'cols' => $this->getCols(),
67  'rows' => $this->getRows(),
68  'spellcheck' => $this->getSpellCheck(),
69  ] + $this->getTooltipAndAccessKey();
70 
71  if ( $this->mClass !== '' ) {
72  $classes[] = $this->mClass;
73  }
74  if ( $this->mUseEditFont ) {
75  $userOptionsLookup = MediaWikiServices::getInstance()
76  ->getUserOptionsLookup();
77  // The following classes can be used here:
78  // * mw-editfont-monospace
79  // * mw-editfont-sans-serif
80  // * mw-editfont-serif
81  $classes[] =
82  'mw-editfont-' .
83  $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
84  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
85  }
86  if ( $this->mPlaceholder !== '' ) {
87  $attribs['placeholder'] = $this->mPlaceholder;
88  }
89  if ( $classes ) {
90  $attribs['class'] = $classes;
91  }
92 
93  $allowedParams = [
94  'tabindex',
95  'disabled',
96  'readonly',
97  'required',
98  'autofocus'
99  ];
100 
101  $attribs += $this->getAttributes( $allowedParams );
102  return Html::textarea( $this->mName, $value, $attribs );
103  }
104 
109  public function getInputOOUI( $value ) {
110  $classes = [];
111 
112  if ( isset( $this->mParams['cols'] ) ) {
113  throw new Exception( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
114  }
115 
116  $attribs = $this->getTooltipAndAccessKeyOOUI();
117 
118  if ( $this->mClass !== '' ) {
119  $classes[] = $this->mClass;
120  }
121  if ( $this->mUseEditFont ) {
122  $userOptionsLookup = MediaWikiServices::getInstance()
123  ->getUserOptionsLookup();
124  // The following classes can be used here:
125  // * mw-editfont-monospace
126  // * mw-editfont-sans-serif
127  // * mw-editfont-serif
128  $classes[] =
129  'mw-editfont-' .
130  $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
131  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
132  }
133  if ( $this->mPlaceholder !== '' ) {
134  $attribs['placeholder'] = $this->mPlaceholder;
135  }
136  if ( count( $classes ) ) {
137  $attribs['classes'] = $classes;
138  }
139 
140  $allowedParams = [
141  'tabindex',
142  'disabled',
143  'readonly',
144  'required',
145  'autofocus',
146  ];
147 
148  $attribs += OOUI\Element::configFromHtmlAttributes(
149  $this->getAttributes( $allowedParams )
150  );
151 
152  return new OOUI\MultilineTextInputWidget( [
153  'id' => $this->mID,
154  'name' => $this->mName,
155  'value' => $value,
156  'rows' => $this->getRows(),
157  ] + $attribs );
158  }
159 }
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 "...
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
Service locator for MediaWiki core services.