HTMLSelectOrOtherField.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Select dropdown field, with an additional "other" textbox.
  4. *
  5. * HTMLComboboxField implements the same functionality using a single form field
  6. * and should be used instead.
  7. */
  8. class HTMLSelectOrOtherField extends HTMLTextField {
  9. public function __construct( $params ) {
  10. parent::__construct( $params );
  11. $this->getOptions();
  12. if ( !in_array( 'other', $this->mOptions, true ) ) {
  13. $msg =
  14. $params['other'] ?? wfMessage( 'htmlform-selectorother-other' )->text();
  15. // Have 'other' always as first element
  16. $this->mOptions = [ $msg => 'other' ] + $this->mOptions;
  17. }
  18. }
  19. public function getInputHTML( $value ) {
  20. $valInSelect = false;
  21. if ( $value !== false ) {
  22. $value = strval( $value );
  23. $valInSelect = in_array(
  24. $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
  25. );
  26. }
  27. $selected = $valInSelect ? $value : 'other';
  28. $select = new XmlSelect( $this->mName, $this->mID, $selected );
  29. $select->addOptions( $this->getOptions() );
  30. $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
  31. $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
  32. if ( !empty( $this->mParams['disabled'] ) ) {
  33. $select->setAttribute( 'disabled', 'disabled' );
  34. $tbAttribs['disabled'] = 'disabled';
  35. }
  36. if ( isset( $this->mParams['tabindex'] ) ) {
  37. $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
  38. $tbAttribs['tabindex'] = $this->mParams['tabindex'];
  39. }
  40. $select = $select->getHTML();
  41. if ( isset( $this->mParams['maxlength'] ) ) {
  42. $tbAttribs['maxlength'] = $this->mParams['maxlength'];
  43. }
  44. if ( $this->mClass !== '' ) {
  45. $tbAttribs['class'] = $this->mClass;
  46. }
  47. $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
  48. return "$select<br />\n$textbox";
  49. }
  50. protected function shouldInfuseOOUI() {
  51. return true;
  52. }
  53. protected function getOOUIModules() {
  54. return [ 'mediawiki.widgets.SelectWithInputWidget' ];
  55. }
  56. public function getInputOOUI( $value ) {
  57. $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
  58. $valInSelect = false;
  59. if ( $value !== false ) {
  60. $value = strval( $value );
  61. $valInSelect = in_array(
  62. $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
  63. );
  64. }
  65. # DropdownInput
  66. $dropdownAttribs = [
  67. 'name' => $this->mName,
  68. 'options' => $this->getOptionsOOUI(),
  69. 'value' => $valInSelect ? $value : 'other',
  70. 'class' => [ 'mw-htmlform-select-or-other' ],
  71. ];
  72. $allowedParams = [
  73. 'disabled',
  74. 'tabindex',
  75. ];
  76. $dropdownAttribs += OOUI\Element::configFromHtmlAttributes(
  77. $this->getAttributes( $allowedParams )
  78. );
  79. # TextInput
  80. $textAttribs = [
  81. 'name' => $this->mName . '-other',
  82. 'size' => $this->getSize(),
  83. 'value' => $valInSelect ? '' : $value,
  84. ];
  85. $allowedParams = [
  86. 'required',
  87. 'autofocus',
  88. 'multiple',
  89. 'disabled',
  90. 'tabindex',
  91. 'maxlength',
  92. ];
  93. $textAttribs += OOUI\Element::configFromHtmlAttributes(
  94. $this->getAttributes( $allowedParams )
  95. );
  96. if ( $this->mClass !== '' ) {
  97. $textAttribs['classes'] = [ $this->mClass ];
  98. }
  99. if ( $this->mPlaceholder !== '' ) {
  100. $textAttribs['placeholder'] = $this->mPlaceholder;
  101. }
  102. $disabled = false;
  103. if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
  104. $disabled = true;
  105. }
  106. return $this->getInputWidget( [
  107. 'id' => $this->mID,
  108. 'disabled' => $disabled,
  109. 'textinput' => $textAttribs,
  110. 'dropdowninput' => $dropdownAttribs,
  111. 'required' => $this->mParams[ 'required' ] ?? false,
  112. 'or' => true,
  113. ] );
  114. }
  115. public function getInputWidget( $params ) {
  116. return new MediaWiki\Widget\SelectWithInputWidget( $params );
  117. }
  118. /**
  119. * @param WebRequest $request
  120. *
  121. * @return string
  122. */
  123. public function loadDataFromRequest( $request ) {
  124. if ( $request->getCheck( $this->mName ) ) {
  125. $val = $request->getText( $this->mName );
  126. if ( $val === 'other' ) {
  127. $val = $request->getText( $this->mName . '-other' );
  128. }
  129. return $val;
  130. } else {
  131. return $this->getDefault();
  132. }
  133. }
  134. }