3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\Html\Html
;
6 use MediaWiki\HTMLForm\HTMLForm
;
7 use MediaWiki\HTMLForm\HTMLFormField
;
8 use MediaWiki\HTMLForm\OOUIHTMLForm
;
9 use MediaWiki\HTMLForm\VFormHTMLForm
;
10 use MediaWiki\Request\WebRequest
;
11 use MediaWiki\Xml\Xml
;
18 class HTMLCheckField
extends HTMLFormField
{
24 public function getInputHTML( $value ) {
25 if ( !empty( $this->mParams
['invert'] ) ) {
29 $attr = $this->getTooltipAndAccessKey();
30 $attr['id'] = $this->mID
;
32 $attr +
= $this->getAttributes( [ 'disabled', 'tabindex' ] );
34 if ( $this->mClass
!== '' ) {
35 $attr['class'] = $this->mClass
;
38 $attrLabel = [ 'for' => $this->mID
];
39 if ( isset( $attr['title'] ) ) {
40 // propagate tooltip to label
41 $attrLabel['title'] = $attr['title'];
44 $isVForm = $this->mParent
instanceof VFormHTMLForm
;
46 $chkDivider = "\u{00A0}";
47 $chkLabel = Xml
::check( $this->mName
, $value, $attr ) .
49 Html
::rawElement( 'label', $attrLabel, $this->mLabel
);
52 $chkLabelClass = 'mw-ui-checkbox';
53 $chkLabel = Html
::rawElement(
55 [ 'class' => $chkLabelClass ],
64 * Get the OOUI version of this field.
67 * @param string $value
68 * @return \OOUI\CheckboxInputWidget The checkbox widget.
70 public function getInputOOUI( $value ) {
71 if ( !empty( $this->mParams
['invert'] ) ) {
75 $attr = $this->getTooltipAndAccessKeyOOUI();
76 $attr['id'] = $this->mID
;
77 $attr['name'] = $this->mName
;
79 $attr +
= \OOUI\Element
::configFromHtmlAttributes(
80 $this->getAttributes( [ 'disabled', 'tabindex' ] )
83 if ( $this->mClass
!== '' ) {
84 $attr['classes'] = [ $this->mClass
];
87 $attr['selected'] = $value;
88 $attr['value'] = '1'; // Nasty hack, but needed to make this work
90 return new \OOUI\
CheckboxInputWidget( $attr );
93 public function getInputCodex( $value, $hasErrors ) {
94 if ( !empty( $this->mParams
['invert'] ) ) {
98 // Attributes for the <input> element.
99 $attribs = $this->getTooltipAndAccessKey();
100 $attribs['id'] = $this->mID
;
101 $attribs +
= $this->getAttributes( [ 'disabled', 'tabindex' ] );
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';
107 // Attributes for the <label> element.
108 $labelAttribs = [ 'for' => $this->mID
];
109 $labelAttribs['class'] = [ 'cdx-checkbox__label' ];
111 // Attributes for the wrapper <div>.
112 $wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ];
114 $wrapperAttribs['class'][] = 'cdx-checkbox--status-error';
116 if ( isset( $attribs['title'] ) ) {
117 // Propagate tooltip to the entire component (including the label).
118 $wrapperAttribs['title'] = $attribs['title'];
121 // Construct the component.
122 $checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>";
123 $innerContent = Xml
::check( $this->mName
, $value, $attribs ) .
125 Html
::rawElement( 'label', $labelAttribs, $this->mLabel
);
126 return Html
::rawElement(
134 * For a checkbox, the label goes on the right hand side, and is
135 * added in getInputHTML(), rather than HTMLFormField::getRow()
137 * ...unless OOUI is being used, in which case we actually return
140 * @stable to override
143 public function getLabel() {
144 if ( $this->mParent
instanceof OOUIHTMLForm
) {
145 return $this->mLabel ??
'';
147 $this->mParent
instanceof HTMLForm
&&
148 $this->mParent
->getDisplayFormat() === 'div'
157 * Get label alignment when generating field for OOUI.
158 * @stable to override
159 * @return string 'left', 'right', 'top' or 'inline'
161 protected function getLabelAlignOOUI() {
166 * checkboxes don't need a label.
167 * @stable to override
170 protected function needsLabel() {
177 public function getDefault() {
178 return (bool)$this->mDefault
;
182 * @stable to override
183 * @param WebRequest $request
187 public function loadDataFromRequest( $request ) {
188 $invert = isset( $this->mParams
['invert'] ) && $this->mParams
['invert'];
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
) ) {
195 ?
!$request->getBool( $this->mName
)
196 : $request->getBool( $this->mName
);
198 return $this->getDefault();
203 /** @deprecated class alias since 1.42 */
204 class_alias( HTMLCheckField
::class, 'HTMLCheckField' );