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