MediaWiki master
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
9
40 protected $autocompleteData = [];
41
46 public function __construct( $params ) {
47 $params += [
48 'require-match' => false,
49 ];
50
51 parent::__construct( $params );
52
53 if ( array_key_exists( 'autocomplete-data-messages', $this->mParams ) ) {
54 foreach ( $this->mParams['autocomplete-data-messages'] as $key => $value ) {
55 $key = $this->msg( $key )->plain();
56 $this->autocompleteData[$key] = strval( $value );
57 }
58 } elseif ( array_key_exists( 'autocomplete-data', $this->mParams ) ) {
59 foreach ( $this->mParams['autocomplete-data'] as $key => $value ) {
60 $this->autocompleteData[$key] = strval( $value );
61 }
62 }
63 if ( !is_array( $this->autocompleteData ) || !$this->autocompleteData ) {
64 throw new InvalidArgumentException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
65 }
66
67 $this->getOptions();
68 if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
69 if ( isset( $params['other-message'] ) ) {
70 $msg = $this->getMessage( $params['other-message'] )->text();
71 } elseif ( isset( $params['other'] ) ) {
72 $msg = $params['other'];
73 } else {
74 $msg = wfMessage( 'htmlform-selectorother-other' )->text();
75 }
76 $this->mOptions[$msg] = 'other';
77 }
78 }
79
81 public function loadDataFromRequest( $request ) {
82 if ( $request->getCheck( $this->mName ) ) {
83 $val = $request->getText( $this->mName . '-select', 'other' );
84
85 if ( $val === 'other' ) {
86 $val = $request->getText( $this->mName );
87 if ( isset( $this->autocompleteData[$val] ) ) {
88 $val = $this->autocompleteData[$val];
89 }
90 }
91
92 return $val;
93 } else {
94 return $this->getDefault();
95 }
96 }
97
99 public function validate( $value, $alldata ) {
100 $p = parent::validate( $value, $alldata );
101
102 if ( $p !== true ) {
103 return $p;
104 }
105
106 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ?: [] );
107
108 if (
109 in_array( strval( $value ), $validOptions, true ) ||
110 in_array( strval( $value ), $this->autocompleteData, true )
111 ) {
112 return true;
113 } elseif ( $this->mParams['require-match'] ) {
114 return $this->msg( 'htmlform-select-badoption' );
115 }
116
117 return true;
118 }
119
121 public function getAttributes( array $list ) {
122 // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
123 $attribs = [
124 'type' => 'text',
125 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocompleteData ) ),
126 ] + parent::getAttributes( $list );
127
128 if ( $this->getOptions() ) {
129 $attribs['data-cond-state'] = FormatJson::encode( [
130 'hide' => [ '!==', $this->mName . '-select', 'other' ],
131 ] );
132 }
133
134 return $attribs;
135 }
136
138 public function getInputHTML( $value ) {
139 $oldClass = $this->mClass;
140 $classes = (array)$this->mClass;
141
142 $valInSelect = false;
143 $ret = '';
144
145 if ( $this->getOptions() ) {
146 if ( $value !== false ) {
147 $value = strval( $value );
148 $valInSelect = in_array(
149 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
150 );
151 }
152
153 $selected = $valInSelect ? $value : 'other';
154 $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
155 $select->addOptions( $this->getOptions() );
156
157 if ( !empty( $this->mParams['disabled'] ) ) {
158 $select->setAttribute( 'disabled', 'disabled' );
159 }
160
161 if ( isset( $this->mParams['tabindex'] ) ) {
162 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
163 }
164
165 $ret = $select->getHTML() . "<br />\n";
166
167 $classes[] = 'mw-htmlform-hide-if';
168 }
169
170 if ( $valInSelect ) {
171 $value = '';
172 } else {
173 $key = array_search( strval( $value ), $this->autocompleteData, true );
174 if ( $key !== false ) {
175 $value = $key;
176 }
177 }
178
179 $classes[] = 'mw-htmlform-autocomplete';
180 $this->mClass = implode( ' ', $classes );
181 $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
182 $this->mClass = $oldClass;
183
184 return $ret;
185 }
186
192 public function getInputOOUI( $value ) {
193 // To be implemented, for now override the function from HTMLTextField
194 return false;
195 }
196}
197
199class_alias( HTMLAutoCompleteSelectField::class, 'HTMLAutoCompleteSelectField' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Text field for selecting a value from a large list of possible values, with auto-completion and optio...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getAttributes(array $list)
Returns the given attributes from the parameters.to overridearray Attributes
getInputOOUI( $value)
Get the OOUI version of this input.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
msg( $key,... $params)
Get a translated interface message.
JSON formatter wrapper class.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:16