MediaWiki master
HTMLRadioField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
9
32 public function __construct( $params ) {
33 parent::__construct( $params );
34
35 if ( isset( $params['flatlist'] ) ) {
36 $this->mClass .= ' mw-htmlform-flatlist';
37 }
38 }
39
40 public function validate( $value, $alldata ) {
41 $p = parent::validate( $value, $alldata );
42
43 if ( $p !== true ) {
44 return $p;
45 }
46
47 if ( !is_string( $value ) && !is_int( $value ) ) {
48 return $this->msg( 'htmlform-required' );
49 }
50
51 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
52
53 if ( in_array( strval( $value ), $validOptions, true ) ) {
54 return true;
55 } else {
56 return $this->msg( 'htmlform-select-badoption' );
57 }
58 }
59
68 public function getInputHTML( $value ) {
69 if (
70 isset( $this->mParams['option-descriptions'] ) ||
71 isset( $this->mParams['option-descriptions-messages'] ) ) {
72 throw new InvalidArgumentException(
73 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
74 );
75 }
76
77 $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
78
79 return $html;
80 }
81
82 public function getInputOOUI( $value ) {
83 if (
84 isset( $this->mParams['option-descriptions'] ) ||
85 isset( $this->mParams['option-descriptions-messages'] ) ) {
86 throw new InvalidArgumentException(
87 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
88 );
89 }
90
91 $options = [];
92 foreach ( $this->getOptions() as $label => $data ) {
93 if ( is_int( $label ) ) {
94 $label = strval( $label );
95 }
96 $options[] = [
97 'data' => $data,
98 'label' => $this->makeLabelSnippet( $label ),
99 ];
100 }
101
102 return new \OOUI\RadioSelectInputWidget( [
103 'name' => $this->mName,
104 'id' => $this->mID,
105 'value' => $value,
106 'options' => $options,
107 ] + \OOUI\Element::configFromHtmlAttributes(
108 $this->getAttributes( [ 'disabled', 'tabindex' ] )
109 ) );
110 }
111
112 public function getInputCodex( $value, $hasErrors ) {
113 $optionDescriptions = $this->getOptionDescriptions();
114 $html = '';
115
116 // Iterate over an array of options and return the HTML markup.
117 foreach ( $this->getOptions() as $label => $radioValue ) {
118 $description = $optionDescriptions[$radioValue] ?? '';
119 $descriptionID = Sanitizer::escapeIdForAttribute(
120 $this->mID . "-$radioValue-description"
121 );
122
123 // Attributes for the radio input.
124 $radioInputClasses = [ 'cdx-radio__input' ];
125 $radioInputAttribs = [
126 'id' => Sanitizer::escapeIdForAttribute( $this->mID . "-$radioValue" ),
127 'type' => 'radio',
128 'name' => $this->mName,
129 'class' => $radioInputClasses,
130 'value' => $radioValue
131 ];
132 $radioInputAttribs += $this->getAttributes( [ 'disabled', 'tabindex' ] );
133 if ( $description ) {
134 $radioInputAttribs['aria-describedby'] = $descriptionID;
135 }
136
137 // Set the selected value as "checked".
138 if ( $radioValue === $value ) {
139 $radioInputAttribs['checked'] = true;
140 }
141
142 // Attributes for the radio icon.
143 $radioIconClasses = [ 'cdx-radio__icon' ];
144 $radioIconAttribs = [
145 'class' => $radioIconClasses,
146 ];
147
148 // Attributes for the radio label.
149 $radioLabelClasses = [ 'cdx-label__label' ];
150 $radioLabelAttribs = [
151 'class' => $radioLabelClasses,
152 'for' => $radioInputAttribs['id']
153 ];
154
155 // HTML markup for radio input, radio icon, and radio label elements.
156 $radioInput = Html::element( 'input', $radioInputAttribs );
157 $radioIcon = Html::element( 'span', $radioIconAttribs );
158 $radioLabel = Html::rawElement( 'label', $radioLabelAttribs,
159 $this->escapeLabel( $label ) );
160
161 $radioDescription = '';
162 if ( isset( $optionDescriptions[$radioValue] ) ) {
163 $radioDescription = Html::rawElement(
164 'span',
165 [ 'id' => $descriptionID, 'class' => 'cdx-label__description' ],
166 $optionDescriptions[$radioValue]
167 );
168 }
169 $radioLabelWrapper = Html::rawElement(
170 'div',
171 [ 'class' => 'cdx-radio__label cdx-label' ],
172 $radioLabel . $radioDescription
173 );
174
175 // HTML markup for CSS-only Codex Radio.
176 $radio = Html::rawElement(
177 'div',
178 [ 'class' => 'cdx-radio' ],
179 $radioInput . $radioIcon . $radioLabelWrapper
180 );
181
182 // Append the Codex Radio HTML markup to the initialized empty string variable.
183 $html .= $radio;
184 }
185
186 return $html;
187 }
188
189 public function formatOptions( $options, $value ) {
190 $html = '';
191
192 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
193
194 # @todo Should this produce an unordered list perhaps?
195 foreach ( $options as $label => $info ) {
196 if ( is_array( $info ) ) {
197 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
198 $html .= $this->formatOptions( $info, $value );
199 } else {
200 $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
201 $classes = [ 'mw-htmlform-flatlist-item' ];
202 $radio = Html::radio(
203 $this->mName,
204 $info === $value,
205 $attribs + [ 'value' => $info, 'id' => $id ]
206 );
207
208 $radio .= "\u{00A0}" .
209 Html::rawElement( 'label', [ 'for' => $id ], $this->escapeLabel( $label ) );
210
211 $html .= ' ' . Html::rawElement(
212 'div',
213 [ 'class' => $classes ],
214 $radio
215 );
216 }
217 }
218
219 return $html;
220 }
221
228 protected function getOptionDescriptions() {
229 if ( array_key_exists( 'option-descriptions-messages', $this->mParams ) ) {
230 $needsParse = $this->mParams['option-descriptions-messages-parse'] ?? false;
231 $optionDescriptions = [];
232 foreach ( $this->mParams['option-descriptions-messages'] as $value => $msgKey ) {
233 $msg = $this->msg( $msgKey );
234 $optionDescriptions[$value] = $needsParse ? $msg->parse() : $msg->escaped();
235 }
236 return $optionDescriptions;
237 } elseif ( array_key_exists( 'option-descriptions', $this->mParams ) ) {
238 return $this->mParams['option-descriptions'];
239 }
240 }
241
242 protected function needsLabel() {
243 return false;
244 }
245}
246
248class_alias( HTMLRadioField::class, 'HTMLRadioField' );
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getInputHTML( $value)
This returns a block of all the radio options, in one cell.
getOptionDescriptions()
Fetch the array of option descriptions from the field's parameters.
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
The parent class to generate form fields.
makeLabelSnippet( $label)
The keys in the array returned by getOptions() can be either HTML or plain text depending on $this->m...
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.
getAttributes(array $list)
Returns the given attributes from the parameters.
msg( $key,... $params)
Get a translated interface message.
escapeLabel( $label)
The keys in the array returned by getOptions() can be either HTML or plain text depending on $this->m...
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
element(SerializerNode $parent, SerializerNode $node, $contents)