MediaWiki master
HTMLCheckField.php
Go to the documentation of this file.
1<?php
2
4
11
18
23 public function getInputHTML( $value ) {
24 if ( !empty( $this->mParams['invert'] ) ) {
25 $value = !$value;
26 }
27
28 $attr = [
29 ...$this->getTooltipAndAccessKey(),
30 'id' => $this->mID,
31 ...$this->getAttributes( [ 'disabled', 'tabindex' ] ),
32 'class' => $this->mClass !== '' ? $this->mClass : null,
33 'checked' => (bool)$value,
34 'type' => 'checkbox',
35 'value' => '1',
36 'name' => $this->mName,
37 ];
38
39 $attrLabel = [ 'for' => $this->mID ];
40 if ( isset( $attr['title'] ) ) {
41 // propagate tooltip to label
42 $attrLabel['title'] = $attr['title'];
43 }
44
45 $isVForm = $this->mParent instanceof VFormHTMLForm;
46
47 $chkDivider = "\u{00A0}";
48 $chkLabel = Html::element( 'input', $attr ) .
49 $chkDivider .
50 Html::rawElement( 'label', $attrLabel, $this->mLabel );
51
52 if ( $isVForm ) {
53 $chkLabelClass = 'mw-ui-checkbox';
54 $chkLabel = Html::rawElement(
55 'div',
56 [ 'class' => $chkLabelClass ],
57 $chkLabel
58 );
59 }
60
61 return $chkLabel;
62 }
63
71 public function getInputOOUI( $value ) {
72 if ( !empty( $this->mParams['invert'] ) ) {
73 $value = !$value;
74 }
75
76 $attr = $this->getTooltipAndAccessKeyOOUI();
77 $attr['id'] = $this->mID;
78 $attr['name'] = $this->mName;
79
80 $attr += \OOUI\Element::configFromHtmlAttributes(
81 $this->getAttributes( [ 'disabled', 'tabindex' ] )
82 );
83
84 if ( $this->mClass !== '' ) {
85 $attr['classes'] = [ $this->mClass ];
86 }
87
88 $attr['selected'] = $value;
89 $attr['value'] = '1'; // Nasty hack, but needed to make this work
90
91 return new \OOUI\CheckboxInputWidget( $attr );
92 }
93
95 public function getInputCodex( $value, $hasErrors ) {
96 if ( !empty( $this->mParams['invert'] ) ) {
97 $value = !$value;
98 }
99
100 // Attributes for the <input> element.
101 $attribs = [
102 ...$this->getTooltipAndAccessKey(),
103 'id' => $this->mID,
104 ...$this->getAttributes( [ 'disabled', 'tabindex' ] ),
105 'class' => $this->mClass . ' cdx-checkbox__input',
106 'checked' => (bool)$value,
107 'type' => 'checkbox',
108 'value' => '1',
109 'name' => $this->mName,
110 ];
111
112 // Attributes for the <label> element.
113 $labelAttribs = [ 'for' => $this->mID ];
114 $labelAttribs['class'] = [ 'cdx-checkbox__label' ];
115
116 // Attributes for the wrapper <div>.
117 $wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ];
118 if ( $hasErrors ) {
119 $wrapperAttribs['class'][] = 'cdx-checkbox--status-error';
120 }
121 if ( isset( $attribs['title'] ) ) {
122 // Propagate tooltip to the entire component (including the label).
123 $wrapperAttribs['title'] = $attribs['title'];
124 }
125
126 // Construct the component.
127 $checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>";
128 $innerContent = Html::element( 'input', $attribs ) .
129 $checkIcon .
130 Html::rawElement( 'label', $labelAttribs, $this->mLabel );
131 return Html::rawElement(
132 'div',
133 $wrapperAttribs,
134 $innerContent
135 );
136 }
137
148 public function getLabel() {
149 if ( $this->mParent instanceof OOUIHTMLForm ) {
150 return $this->mLabel ?? '';
151 } elseif (
152 $this->mParent instanceof HTMLForm &&
153 $this->mParent->getDisplayFormat() === 'div'
154 ) {
155 return '';
156 } else {
157 return "\u{00A0}";
158 }
159 }
160
166 protected function getLabelAlignOOUI() {
167 return 'inline';
168 }
169
175 protected function needsLabel() {
176 return false;
177 }
178
182 public function getDefault() {
183 return (bool)$this->mDefault;
184 }
185
192 public function loadDataFromRequest( $request ) {
193 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
194
195 // Fetch the value in either one of the two following case:
196 // - we have a valid submit attempt (form was just submitted)
197 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
198 if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
199 return $invert
200 ? !$request->getBool( $this->mName )
201 : $request->getBool( $this->mName );
202 } else {
203 return $this->getDefault();
204 }
205 }
206}
207
209class_alias( HTMLCheckField::class, 'HTMLCheckField' );
getLabelAlignOOUI()
Get label alignment when generating field for OOUI.
getInputOOUI( $value)
Get the OOUI version of this field.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
needsLabel()
checkboxes don't need a label.
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
getLabel()
For a checkbox, the label goes on the right hand side, and is added in getInputHTML(),...
The parent class to generate form fields.
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
getAttributes(array $list)
Returns the given attributes from the parameters.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
getDisplayFormat()
Getter for displayFormat.
Definition HTMLForm.php:572
Compact stacked vertical format for forms, implemented using OOUI widgets.
Compact stacked vertical format for forms.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
element(SerializerNode $parent, SerializerNode $node, $contents)