MediaWiki  1.34.0
HTMLTextAreaField.php
Go to the documentation of this file.
1 <?php
2 
4  const DEFAULT_COLS = 80;
5  const DEFAULT_ROWS = 25;
6 
7  protected $mPlaceholder = '';
8  protected $mUseEditFont = false;
9 
17  public function __construct( $params ) {
18  parent::__construct( $params );
19 
20  if ( isset( $params['placeholder-message'] ) ) {
21  $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
22  } elseif ( isset( $params['placeholder'] ) ) {
23  $this->mPlaceholder = $params['placeholder'];
24  }
25 
26  if ( isset( $params['useeditfont'] ) ) {
27  $this->mUseEditFont = $params['useeditfont'];
28  }
29  }
30 
31  public function getCols() {
32  return $this->mParams['cols'] ?? static::DEFAULT_COLS;
33  }
34 
35  public function getRows() {
36  return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
37  }
38 
39  public function getSpellCheck() {
40  $val = $this->mParams['spellcheck'] ?? null;
41  if ( is_bool( $val ) ) {
42  // "spellcheck" attribute literally requires "true" or "false" to work.
43  return $val === true ? 'true' : 'false';
44  }
45  return null;
46  }
47 
48  public function getInputHTML( $value ) {
49  $classes = [];
50 
51  $attribs = [
52  'id' => $this->mID,
53  'cols' => $this->getCols(),
54  'rows' => $this->getRows(),
55  'spellcheck' => $this->getSpellCheck(),
56  ] + $this->getTooltipAndAccessKey();
57 
58  if ( $this->mClass !== '' ) {
59  array_push( $classes, $this->mClass );
60  }
61  if ( $this->mUseEditFont ) {
62  // The following classes can be used here:
63  // * mw-editfont-monospace
64  // * mw-editfont-sans-serif
65  // * mw-editfont-serif
66  array_push(
67  $classes,
68  'mw-editfont-' . $this->mParent->getUser()->getOption( 'editfont' )
69  );
70  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
71  }
72  if ( $this->mPlaceholder !== '' ) {
73  $attribs['placeholder'] = $this->mPlaceholder;
74  }
75  if ( count( $classes ) ) {
76  $attribs['class'] = implode( ' ', $classes );
77  }
78 
79  $allowedParams = [
80  'tabindex',
81  'disabled',
82  'readonly',
83  'required',
84  'autofocus'
85  ];
86 
87  $attribs += $this->getAttributes( $allowedParams );
88  return Html::textarea( $this->mName, $value, $attribs );
89  }
90 
91  function getInputOOUI( $value ) {
92  $classes = [];
93 
94  if ( isset( $this->mParams['cols'] ) ) {
95  throw new Exception( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
96  }
97 
98  $attribs = $this->getTooltipAndAccessKeyOOUI();
99 
100  if ( $this->mClass !== '' ) {
101  array_push( $classes, $this->mClass );
102  }
103  if ( $this->mUseEditFont ) {
104  // The following classes can be used here:
105  // * mw-editfont-monospace
106  // * mw-editfont-sans-serif
107  // * mw-editfont-serif
108  array_push(
109  $classes,
110  'mw-editfont-' . $this->mParent->getUser()->getOption( 'editfont' )
111  );
112  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
113  }
114  if ( $this->mPlaceholder !== '' ) {
115  $attribs['placeholder'] = $this->mPlaceholder;
116  }
117  if ( count( $classes ) ) {
118  $attribs['classes'] = $classes;
119  }
120 
121  $allowedParams = [
122  'tabindex',
123  'disabled',
124  'readonly',
125  'required',
126  'autofocus',
127  ];
128 
129  $attribs += OOUI\Element::configFromHtmlAttributes(
130  $this->getAttributes( $allowedParams )
131  );
132 
133  return new OOUI\MultilineTextInputWidget( [
134  'id' => $this->mID,
135  'name' => $this->mName,
136  'value' => $value,
137  'rows' => $this->getRows(),
138  ] + $attribs );
139  }
140 }
HTMLTextAreaField\$mUseEditFont
$mUseEditFont
Definition: HTMLTextAreaField.php:8
HTMLFormField\getTooltipAndAccessKeyOOUI
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
Definition: HTMLFormField.php:981
HTMLTextAreaField\getSpellCheck
getSpellCheck()
Definition: HTMLTextAreaField.php:39
HTMLTextAreaField\DEFAULT_COLS
const DEFAULT_COLS
Definition: HTMLTextAreaField.php:4
HTMLFormField\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLFormField.php:1154
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
HTMLTextAreaField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLTextAreaField.php:48
HTMLTextAreaField\getCols
getCols()
Definition: HTMLTextAreaField.php:31
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:16
HTMLTextAreaField\$mPlaceholder
$mPlaceholder
Definition: HTMLTextAreaField.php:7
HTMLTextAreaField\DEFAULT_ROWS
const DEFAULT_ROWS
Definition: HTMLTextAreaField.php:5
HTMLTextAreaField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLTextAreaField.php:91
HTMLTextAreaField\getRows
getRows()
Definition: HTMLTextAreaField.php:35
HTMLFormField\getTooltipAndAccessKey
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
Definition: HTMLFormField.php:968
HTMLTextAreaField\__construct
__construct( $params)
Definition: HTMLTextAreaField.php:17
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:998
HTMLTextAreaField
Definition: HTMLTextAreaField.php:3