MediaWiki master
HTMLCheckField.php
Go to the documentation of this file.
1<?php
2
4
12use Xml;
13
20
25 public function getInputHTML( $value ) {
26 if ( !empty( $this->mParams['invert'] ) ) {
27 $value = !$value;
28 }
29
30 $attr = $this->getTooltipAndAccessKey();
31 $attr['id'] = $this->mID;
32
33 $attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
34
35 if ( $this->mClass !== '' ) {
36 $attr['class'] = $this->mClass;
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 $isCodexForm = $this->mParent instanceof CodexHTMLForm;
46 $isVForm = $this->mParent instanceof VFormHTMLForm;
47
48 if ( $isCodexForm ) {
49 $attrClass = $attr['class'] ?? '';
50 $attr['class'] = $attrClass . ' cdx-checkbox__input';
51 $attrLabel['class'] = 'cdx-checkbox__label';
52 }
53 $chkDivider = $isCodexForm ?
54 "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>" :
55 "\u{00A0}";
56 $chkLabel = Xml::check( $this->mName, $value, $attr ) .
57 $chkDivider .
58 Html::rawElement( 'label', $attrLabel, $this->mLabel );
59
60 if ( $isCodexForm || $isVForm ) {
61 $chkLabelClass = $isCodexForm ? 'cdx-checkbox' : 'mw-ui-checkbox';
62 $chkLabel = Html::rawElement(
63 'div',
64 [ 'class' => $chkLabelClass ],
65 $chkLabel
66 );
67 }
68
69 return $chkLabel;
70 }
71
79 public function getInputOOUI( $value ) {
80 if ( !empty( $this->mParams['invert'] ) ) {
81 $value = !$value;
82 }
83
84 $attr = $this->getTooltipAndAccessKeyOOUI();
85 $attr['id'] = $this->mID;
86 $attr['name'] = $this->mName;
87
88 $attr += \OOUI\Element::configFromHtmlAttributes(
89 $this->getAttributes( [ 'disabled', 'tabindex' ] )
90 );
91
92 if ( $this->mClass !== '' ) {
93 $attr['classes'] = [ $this->mClass ];
94 }
95
96 $attr['selected'] = $value;
97 $attr['value'] = '1'; // Nasty hack, but needed to make this work
98
99 return new \OOUI\CheckboxInputWidget( $attr );
100 }
101
112 public function getLabel() {
113 if ( $this->mParent instanceof OOUIHTMLForm ) {
114 return $this->mLabel ?? '';
115 } elseif (
116 $this->mParent instanceof HTMLForm &&
117 $this->mParent->getDisplayFormat() === 'div'
118 ) {
119 return '';
120 } else {
121 return "\u{00A0}";
122 }
123 }
124
130 protected function getLabelAlignOOUI() {
131 return 'inline';
132 }
133
139 protected function needsLabel() {
140 return false;
141 }
142
146 public function getDefault() {
147 return (bool)$this->mDefault;
148 }
149
156 public function loadDataFromRequest( $request ) {
157 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
158
159 // Fetch the value in either one of the two following case:
160 // - we have a valid submit attempt (form was just submitted)
161 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
162 if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
163 return $invert
164 ? !$request->getBool( $this->mName )
165 : $request->getBool( $this->mName );
166 } else {
167 return $this->getDefault();
168 }
169 }
170}
171
173class_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.
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:556
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 stripping il...
Module of static functions for generating XML.
Definition Xml.php:33