1<?php 2 3/* 4 * @stable to extend 5 */ 6class HTMLTextAreaField extends HTMLFormField { 7 protected const DEFAULT_COLS = 80; 8 protected const DEFAULT_ROWS = 25; 9 10 protected $mPlaceholder = ''; 11 protected $mUseEditFont = false; 12 13 /** 14 * @stable to call 15 * 16 * @param array $params 17 * - cols, rows: textarea size 18 * - placeholder/placeholder-message: set HTML placeholder attribute 19 * - spellcheck: set HTML spellcheck attribute 20 * - useeditfont: add CSS classes to use the same font as the wikitext editor 21 */ 22 public function __construct( $params ) { 23 parent::__construct( $params ); 24 25 if ( isset( $params['placeholder-message'] ) ) { 26 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text(); 27 } elseif ( isset( $params['placeholder'] ) ) { 28 $this->mPlaceholder = $params['placeholder']; 29 } 30 31 if ( isset( $params['useeditfont'] ) ) { 32 $this->mUseEditFont = $params['useeditfont']; 33 } 34 } 35 36 public function getCols() { 37 return $this->mParams['cols'] ?? static::DEFAULT_COLS; 38 } 39 40 public function getRows() { 41 return $this->mParams['rows'] ?? static::DEFAULT_ROWS; 42 } 43 44 public function getSpellCheck() { 45 $val = $this->mParams['spellcheck'] ?? null; 46 if ( is_bool( $val ) ) { 47 // "spellcheck" attribute literally requires "true" or "false" to work. 48 return $val ? 'true' : 'false'; 49 } 50 return null; 51 } 52 53 /** 54 * @inheritDoc 55 * @stable to override 56 */ 57 public function getInputHTML( $value ) { 58 $classes = []; 59 60 $attribs = [ 61 'id' => $this->mID, 62 'cols' => $this->getCols(), 63 'rows' => $this->getRows(), 64 'spellcheck' => $this->getSpellCheck(), 65 ] + $this->getTooltipAndAccessKey(); 66 67 if ( $this->mClass !== '' ) { 68 array_push( $classes, $this->mClass ); 69 } 70 if ( $this->mUseEditFont ) { 71 // The following classes can be used here: 72 // * mw-editfont-monospace 73 // * mw-editfont-sans-serif 74 // * mw-editfont-serif 75 array_push( 76 $classes, 77 'mw-editfont-' . $this->mParent->getUser()->getOption( 'editfont' ) 78 ); 79 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' ); 80 } 81 if ( $this->mPlaceholder !== '' ) { 82 $attribs['placeholder'] = $this->mPlaceholder; 83 } 84 if ( $classes ) { 85 $attribs['class'] = $classes; 86 } 87 88 $allowedParams = [ 89 'tabindex', 90 'disabled', 91 'readonly', 92 'required', 93 'autofocus' 94 ]; 95 96 $attribs += $this->getAttributes( $allowedParams ); 97 return Html::textarea( $this->mName, $value, $attribs ); 98 } 99 100 /** 101 * @inheritDoc 102 * @stable to override 103 */ 104 public function getInputOOUI( $value ) { 105 $classes = []; 106 107 if ( isset( $this->mParams['cols'] ) ) { 108 throw new Exception( "OOUIHTMLForm does not support the 'cols' parameter for textareas" ); 109 } 110 111 $attribs = $this->getTooltipAndAccessKeyOOUI(); 112 113 if ( $this->mClass !== '' ) { 114 array_push( $classes, $this->mClass ); 115 } 116 if ( $this->mUseEditFont ) { 117 // The following classes can be used here: 118 // * mw-editfont-monospace 119 // * mw-editfont-sans-serif 120 // * mw-editfont-serif 121 array_push( 122 $classes, 123 'mw-editfont-' . $this->mParent->getUser()->getOption( 'editfont' ) 124 ); 125 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' ); 126 } 127 if ( $this->mPlaceholder !== '' ) { 128 $attribs['placeholder'] = $this->mPlaceholder; 129 } 130 if ( count( $classes ) ) { 131 $attribs['classes'] = $classes; 132 } 133 134 $allowedParams = [ 135 'tabindex', 136 'disabled', 137 'readonly', 138 'required', 139 'autofocus', 140 ]; 141 142 $attribs += OOUI\Element::configFromHtmlAttributes( 143 $this->getAttributes( $allowedParams ) 144 ); 145 146 return new OOUI\MultilineTextInputWidget( [ 147 'id' => $this->mID, 148 'name' => $this->mName, 149 'value' => $value, 150 'rows' => $this->getRows(), 151 ] + $attribs ); 152 } 153} 154