Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / htmlform / fields / HTMLSelectOrOtherField.php
blobbb410799ef6c7a97b29f15e14eb012d170d2ae93
1 <?php
3 /**
4 * Select dropdown field, with an additional "other" textbox.
6 * HTMLComboboxField implements the same functionality using a single form field
7 * and should be used instead.
8 */
9 class HTMLSelectOrOtherField extends HTMLTextField {
10 public function __construct( $params ) {
11 parent::__construct( $params );
12 $this->getOptions();
13 if ( !in_array( 'other', $this->mOptions, true ) ) {
14 $msg =
15 isset( $params['other'] )
16 ? $params['other']
17 : wfMessage( 'htmlform-selectorother-other' )->text();
18 // Have 'other' always as first element
19 $this->mOptions = [ $msg => 'other' ] + $this->mOptions;
23 public function getInputHTML( $value ) {
24 $valInSelect = false;
26 if ( $value !== false ) {
27 $value = strval( $value );
28 $valInSelect = in_array(
29 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
33 $selected = $valInSelect ? $value : 'other';
35 $select = new XmlSelect( $this->mName, $this->mID, $selected );
36 $select->addOptions( $this->getOptions() );
38 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
40 $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
42 if ( !empty( $this->mParams['disabled'] ) ) {
43 $select->setAttribute( 'disabled', 'disabled' );
44 $tbAttribs['disabled'] = 'disabled';
47 if ( isset( $this->mParams['tabindex'] ) ) {
48 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
49 $tbAttribs['tabindex'] = $this->mParams['tabindex'];
52 $select = $select->getHTML();
54 if ( isset( $this->mParams['maxlength'] ) ) {
55 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
58 if ( $this->mClass !== '' ) {
59 $tbAttribs['class'] = $this->mClass;
62 $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
64 return "$select<br />\n$textbox";
67 public function getInputOOUI( $value ) {
68 return false;
71 /**
72 * @param WebRequest $request
74 * @return string
76 public function loadDataFromRequest( $request ) {
77 if ( $request->getCheck( $this->mName ) ) {
78 $val = $request->getText( $this->mName );
80 if ( $val === 'other' ) {
81 $val = $request->getText( $this->mName . '-other' );
84 return $val;
85 } else {
86 return $this->getDefault();