MediaWiki master
HTMLCheckField.php
Go to the documentation of this file.
1<?php
2
4
10
17
22 public function getInputHTML( $value ) {
23 if ( !empty( $this->mParams['invert'] ) ) {
24 $value = !$value;
25 }
26
27 $attr = [
28 ...$this->getTooltipAndAccessKey(),
29 'id' => $this->mID,
30 ...$this->getAttributes( [ 'disabled', 'tabindex' ] ),
31 'class' => $this->mClass !== '' ? $this->mClass : null,
32 'checked' => (bool)$value,
33 'type' => 'checkbox',
34 'value' => '1',
35 'name' => $this->mName,
36 ];
37
38 $attrLabel = [ 'for' => $this->mID ];
39 if ( isset( $attr['title'] ) ) {
40 // propagate tooltip to label
41 $attrLabel['title'] = $attr['title'];
42 }
43
44 $chkDivider = "\u{00A0}";
45 $chkLabel = Html::element( 'input', $attr ) .
46 $chkDivider .
47 Html::rawElement( 'label', $attrLabel, $this->mLabel );
48
49 return $chkLabel;
50 }
51
59 public function getInputOOUI( $value ) {
60 if ( !empty( $this->mParams['invert'] ) ) {
61 $value = !$value;
62 }
63
64 $attr = $this->getTooltipAndAccessKeyOOUI();
65 $attr['id'] = $this->mID;
66 $attr['name'] = $this->mName;
67
68 $attr += \OOUI\Element::configFromHtmlAttributes(
69 $this->getAttributes( [ 'disabled', 'tabindex' ] )
70 );
71
72 if ( $this->mClass !== '' ) {
73 $attr['classes'] = [ $this->mClass ];
74 }
75
76 $attr['selected'] = $value;
77 $attr['value'] = '1'; // Nasty hack, but needed to make this work
78
79 return new \OOUI\CheckboxInputWidget( $attr );
80 }
81
83 public function getInputCodex( $value, $hasErrors ) {
84 if ( !empty( $this->mParams['invert'] ) ) {
85 $value = !$value;
86 }
87
88 // Attributes for the <input> element.
89 $attribs = [
90 ...$this->getTooltipAndAccessKey(),
91 'id' => $this->mID,
92 ...$this->getAttributes( [ 'disabled', 'tabindex' ] ),
93 'class' => $this->mClass . ' cdx-checkbox__input',
94 'checked' => (bool)$value,
95 'type' => 'checkbox',
96 'value' => '1',
97 'name' => $this->mName,
98 ];
99
100 // Attributes for the <label> element.
101 $labelAttribs = [ 'for' => $this->mID ];
102 $labelAttribs['class'] = [ 'cdx-checkbox__label' ];
103
104 // Attributes for the wrapper <div>.
105 $wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ];
106 if ( $hasErrors ) {
107 $wrapperAttribs['class'][] = 'cdx-checkbox--status-error';
108 }
109 if ( isset( $attribs['title'] ) ) {
110 // Propagate tooltip to the entire component (including the label).
111 $wrapperAttribs['title'] = $attribs['title'];
112 }
113
114 // Construct the component.
115 $checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>";
116 $innerContent = Html::element( 'input', $attribs ) .
117 $checkIcon .
118 Html::rawElement( 'label', $labelAttribs, $this->mLabel );
119 return Html::rawElement(
120 'div',
121 $wrapperAttribs,
122 $innerContent
123 );
124 }
125
136 public function getLabel() {
137 if ( $this->mParent instanceof OOUIHTMLForm ) {
138 return $this->mLabel ?? '';
139 } elseif (
140 $this->mParent instanceof HTMLForm &&
141 $this->mParent->getDisplayFormat() === 'div'
142 ) {
143 return '';
144 } else {
145 return "\u{00A0}";
146 }
147 }
148
154 protected function getLabelAlignOOUI() {
155 return 'inline';
156 }
157
163 protected function needsLabel() {
164 return false;
165 }
166
170 public function getDefault() {
171 return (bool)$this->mDefault;
172 }
173
180 public function loadDataFromRequest( $request ) {
181 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
182
183 // Fetch the value in either one of the two following case:
184 // - we have a valid submit attempt (form was just submitted)
185 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
186 if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
187 return $invert
188 ? !$request->getBool( $this->mName )
189 : $request->getBool( $this->mName );
190 } else {
191 return $this->getDefault();
192 }
193 }
194}
195
197class_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:208
getDisplayFormat()
Getter for displayFormat.
Definition HTMLForm.php:582
Compact stacked vertical format for forms, implemented using OOUI widgets.
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
element(SerializerNode $parent, SerializerNode $node, $contents)