HTMLTextAreaField.php 3.5 KB

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