MediaWiki  1.34.0
HTMLSelectOrOtherField.php
Go to the documentation of this file.
1 <?php
2 
10  public function __construct( $params ) {
11  parent::__construct( $params );
12  $this->getOptions();
13  if ( !in_array( 'other', $this->mOptions, true ) ) {
14  $msg =
15  $params['other'] ?? wfMessage( 'htmlform-selectorother-other' )->text();
16  // Have 'other' always as first element
17  $this->mOptions = [ $msg => 'other' ] + $this->mOptions;
18  }
19  }
20 
21  public function getInputHTML( $value ) {
22  $valInSelect = false;
23 
24  if ( $value !== false ) {
25  $value = strval( $value );
26  $valInSelect = in_array(
27  $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
28  );
29  }
30 
31  $selected = $valInSelect ? $value : 'other';
32 
33  $select = new XmlSelect( $this->mName, $this->mID, $selected );
34  $select->addOptions( $this->getOptions() );
35 
36  $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
37 
38  $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
39 
40  if ( !empty( $this->mParams['disabled'] ) ) {
41  $select->setAttribute( 'disabled', 'disabled' );
42  $tbAttribs['disabled'] = 'disabled';
43  }
44 
45  if ( isset( $this->mParams['tabindex'] ) ) {
46  $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
47  $tbAttribs['tabindex'] = $this->mParams['tabindex'];
48  }
49 
50  $select = $select->getHTML();
51 
52  if ( isset( $this->mParams['maxlength'] ) ) {
53  $tbAttribs['maxlength'] = $this->mParams['maxlength'];
54  }
55 
56  if ( $this->mClass !== '' ) {
57  $tbAttribs['class'] = $this->mClass;
58  }
59 
60  $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
61 
62  return "$select<br />\n$textbox";
63  }
64 
65  protected function shouldInfuseOOUI() {
66  return true;
67  }
68 
69  protected function getOOUIModules() {
70  return [ 'mediawiki.widgets.SelectWithInputWidget' ];
71  }
72 
73  public function getInputOOUI( $value ) {
74  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
75 
76  $valInSelect = false;
77  if ( $value !== false ) {
78  $value = strval( $value );
79  $valInSelect = in_array(
80  $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
81  );
82  }
83 
84  # DropdownInput
85  $dropdownAttribs = [
86  'name' => $this->mName,
87  'options' => $this->getOptionsOOUI(),
88  'value' => $valInSelect ? $value : 'other',
89  'class' => [ 'mw-htmlform-select-or-other' ],
90  ];
91 
92  $allowedParams = [
93  'disabled',
94  'tabindex',
95  ];
96 
97  $dropdownAttribs += OOUI\Element::configFromHtmlAttributes(
98  $this->getAttributes( $allowedParams )
99  );
100 
101  # TextInput
102  $textAttribs = [
103  'name' => $this->mName . '-other',
104  'size' => $this->getSize(),
105  'value' => $valInSelect ? '' : $value,
106  ];
107 
108  $allowedParams = [
109  'required',
110  'autofocus',
111  'multiple',
112  'disabled',
113  'tabindex',
114  'maxlength',
115  ];
116 
117  $textAttribs += OOUI\Element::configFromHtmlAttributes(
118  $this->getAttributes( $allowedParams )
119  );
120 
121  if ( $this->mClass !== '' ) {
122  $textAttribs['classes'] = [ $this->mClass ];
123  }
124  if ( $this->mPlaceholder !== '' ) {
125  $textAttribs['placeholder'] = $this->mPlaceholder;
126  }
127 
128  $disabled = false;
129  if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
130  $disabled = true;
131  }
132 
133  return $this->getInputWidget( [
134  'id' => $this->mID,
135  'disabled' => $disabled,
136  'textinput' => $textAttribs,
137  'dropdowninput' => $dropdownAttribs,
138  'required' => $this->mParams[ 'required' ] ?? false,
139  'or' => true,
140  ] );
141  }
142 
143  public function getInputWidget( $params ) {
144  return new MediaWiki\Widget\SelectWithInputWidget( $params );
145  }
146 
152  public function loadDataFromRequest( $request ) {
153  if ( $request->getCheck( $this->mName ) ) {
154  $val = $request->getText( $this->mName );
155 
156  if ( $val === 'other' ) {
157  $val = $request->getText( $this->mName . '-other' );
158  }
159 
160  return $val;
161  } else {
162  return $this->getDefault();
163  }
164  }
165 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1054
HTMLSelectOrOtherField\getInputWidget
getInputWidget( $params)
Definition: HTMLSelectOrOtherField.php:143
HTMLSelectOrOtherField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLSelectOrOtherField.php:73
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
HTMLSelectOrOtherField
Select dropdown field, with an additional "other" textbox.
Definition: HTMLSelectOrOtherField.php:9
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:17
MediaWiki\Widget\SelectWithInputWidget
Select and input widget.
Definition: SelectWithInputWidget.php:14
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
HTMLFormField\$mOptions
array bool null $mOptions
Definition: HTMLFormField.php:24
HTMLTextField\$mPlaceholder
$mPlaceholder
Definition: HTMLTextField.php:12
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:13
HTMLSelectOrOtherField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLSelectOrOtherField.php:152
HTMLSelectOrOtherField\__construct
__construct( $params)
Definition: HTMLSelectOrOtherField.php:10
HTMLSelectOrOtherField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLSelectOrOtherField.php:65
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:959
HTMLSelectOrOtherField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLSelectOrOtherField.php:69
HTMLSelectOrOtherField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLSelectOrOtherField.php:21
HTMLFormField\flattenOptions
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Definition: HTMLFormField.php:1093
HTMLTextField\getSize
getSize()
Definition: HTMLTextField.php:40
HTMLFormField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLFormField.php:1076
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:998