MediaWiki 1.42.1
HTMLCheckField.php
Go to the documentation of this file.
1<?php
2
4
11use Xml;
12
19
24 public function getInputHTML( $value ) {
25 if ( !empty( $this->mParams['invert'] ) ) {
26 $value = !$value;
27 }
28
29 $attr = $this->getTooltipAndAccessKey();
30 $attr['id'] = $this->mID;
31
32 $attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
33
34 if ( $this->mClass !== '' ) {
35 $attr['class'] = $this->mClass;
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 $isVForm = $this->mParent instanceof VFormHTMLForm;
45
46 $chkDivider = "\u{00A0}";
47 $chkLabel = Xml::check( $this->mName, $value, $attr ) .
48 $chkDivider .
49 Html::rawElement( 'label', $attrLabel, $this->mLabel );
50
51 if ( $isVForm ) {
52 $chkLabelClass = 'mw-ui-checkbox';
53 $chkLabel = Html::rawElement(
54 'div',
55 [ 'class' => $chkLabelClass ],
56 $chkLabel
57 );
58 }
59
60 return $chkLabel;
61 }
62
70 public function getInputOOUI( $value ) {
71 if ( !empty( $this->mParams['invert'] ) ) {
72 $value = !$value;
73 }
74
75 $attr = $this->getTooltipAndAccessKeyOOUI();
76 $attr['id'] = $this->mID;
77 $attr['name'] = $this->mName;
78
79 $attr += \OOUI\Element::configFromHtmlAttributes(
80 $this->getAttributes( [ 'disabled', 'tabindex' ] )
81 );
82
83 if ( $this->mClass !== '' ) {
84 $attr['classes'] = [ $this->mClass ];
85 }
86
87 $attr['selected'] = $value;
88 $attr['value'] = '1'; // Nasty hack, but needed to make this work
89
90 return new \OOUI\CheckboxInputWidget( $attr );
91 }
92
93 public function getInputCodex( $value, $hasErrors ) {
94 if ( !empty( $this->mParams['invert'] ) ) {
95 $value = !$value;
96 }
97
98 // Attributes for the <input> element.
99 $attribs = $this->getTooltipAndAccessKey();
100 $attribs['id'] = $this->mID;
101 $attribs += $this->getAttributes( [ 'disabled', 'tabindex' ] );
102
103 // The Xml class doesn't support an array of classes, so we have to provide a string.
104 $inputClass = $this->mClass ?? '';
105 $attribs['class'] = $inputClass . ' cdx-checkbox__input';
106
107 // Attributes for the <label> element.
108 $labelAttribs = [ 'for' => $this->mID ];
109 $labelAttribs['class'] = [ 'cdx-checkbox__label' ];
110
111 // Attributes for the wrapper <div>.
112 $wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ];
113 if ( $hasErrors ) {
114 $wrapperAttribs['class'][] = 'cdx-checkbox--status-error';
115 }
116 if ( isset( $attribs['title'] ) ) {
117 // Propagate tooltip to the entire component (including the label).
118 $wrapperAttribs['title'] = $attribs['title'];
119 }
120
121 // Construct the component.
122 $checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>";
123 $innerContent = Xml::check( $this->mName, $value, $attribs ) .
124 $checkIcon .
125 Html::rawElement( 'label', $labelAttribs, $this->mLabel );
126 return Html::rawElement(
127 'div',
128 $wrapperAttribs,
129 $innerContent
130 );
131 }
132
143 public function getLabel() {
144 if ( $this->mParent instanceof OOUIHTMLForm ) {
145 return $this->mLabel ?? '';
146 } elseif (
147 $this->mParent instanceof HTMLForm &&
148 $this->mParent->getDisplayFormat() === 'div'
149 ) {
150 return '';
151 } else {
152 return "\u{00A0}";
153 }
154 }
155
161 protected function getLabelAlignOOUI() {
162 return 'inline';
163 }
164
170 protected function needsLabel() {
171 return false;
172 }
173
177 public function getDefault() {
178 return (bool)$this->mDefault;
179 }
180
187 public function loadDataFromRequest( $request ) {
188 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
189
190 // Fetch the value in either one of the two following case:
191 // - we have a valid submit attempt (form was just submitted)
192 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
193 if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
194 return $invert
195 ? !$request->getBool( $this->mName )
196 : $request->getBool( $this->mName );
197 } else {
198 return $this->getDefault();
199 }
200 }
201}
202
204class_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.
getLabel()
For a checkbox, the label goes on the right hand side, and is added in getInputHTML(),...
The parent class to generate form fields.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
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:206
getDisplayFormat()
Getter for displayFormat.
Definition HTMLForm.php:555
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:56
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Module of static functions for generating XML.
Definition Xml.php:33