MediaWiki  master
HTMLCheckField.php
Go to the documentation of this file.
1 <?php
2 
6 
13 
18  public function getInputHTML( $value ) {
19  $useMediaWikiUIEverywhere = false;
20  if ( $this->mParent ) {
21  $useMediaWikiUIEverywhere = $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
22  }
23 
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  $isCodexForm = $this->mParent instanceof CodexHTMLForm;
44  $isVForm = $this->mParent instanceof VFormHTMLForm;
45 
46  if ( $isCodexForm ) {
47  $attrClass = $attr['class'] ?? '';
48  $attr['class'] = $attrClass . ' cdx-checkbox__input';
49  $attrLabel['class'] = 'cdx-checkbox__label';
50  }
51  $chkDivider = $isCodexForm ?
52  "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>" :
53  "\u{00A0}";
54  $chkLabel = Xml::check( $this->mName, $value, $attr ) .
55  $chkDivider .
56  Html::rawElement( 'label', $attrLabel, $this->mLabel );
57 
58  if ( $isCodexForm || $useMediaWikiUIEverywhere || $isVForm ) {
59  $chkLabelClass = $isCodexForm ? 'cdx-checkbox' : 'mw-ui-checkbox';
60  $chkLabel = Html::rawElement(
61  'div',
62  [ 'class' => $chkLabelClass ],
63  $chkLabel
64  );
65  }
66 
67  return $chkLabel;
68  }
69 
77  public function getInputOOUI( $value ) {
78  if ( !empty( $this->mParams['invert'] ) ) {
79  $value = !$value;
80  }
81 
82  $attr = $this->getTooltipAndAccessKeyOOUI();
83  $attr['id'] = $this->mID;
84  $attr['name'] = $this->mName;
85 
86  $attr += OOUI\Element::configFromHtmlAttributes(
87  $this->getAttributes( [ 'disabled', 'tabindex' ] )
88  );
89 
90  if ( $this->mClass !== '' ) {
91  $attr['classes'] = [ $this->mClass ];
92  }
93 
94  $attr['selected'] = $value;
95  $attr['value'] = '1'; // Nasty hack, but needed to make this work
96 
97  return new OOUI\CheckboxInputWidget( $attr );
98  }
99 
110  public function getLabel() {
111  if ( $this->mParent instanceof OOUIHTMLForm ) {
112  return $this->mLabel ?? '';
113  } elseif (
114  $this->mParent instanceof HTMLForm &&
115  $this->mParent->getDisplayFormat() === 'div'
116  ) {
117  return '';
118  } else {
119  return "\u{00A0}";
120  }
121  }
122 
128  protected function getLabelAlignOOUI() {
129  return 'inline';
130  }
131 
137  protected function needsLabel() {
138  return false;
139  }
140 
144  public function getDefault() {
145  return (bool)$this->mDefault;
146  }
147 
154  public function loadDataFromRequest( $request ) {
155  $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
156 
157  // Fetch the value in either one of the two following case:
158  // - we have a valid submit attempt (form was just submitted)
159  // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
160  if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
161  return $invert
162  ? !$request->getBool( $this->mName )
163  : $request->getBool( $this->mName );
164  } else {
165  return $this->getDefault();
166  }
167  }
168 }
Codex based HTML form.
A checkbox field.
getLabel()
For a checkbox, the label goes on the right hand side, and is added in getInputHTML(),...
loadDataFromRequest( $request)
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getLabelAlignOOUI()
Get label alignment when generating field for OOUI.
getInputOOUI( $value)
Get the OOUI version of this field.
needsLabel()
checkboxes don't need a label.
The parent class to generate form fields.
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
getAttributes(array $list)
Returns the given attributes from the parameters.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:158
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:509
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
A class containing constants representing the names of configuration variables.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:50
Compact stacked vertical format for forms, implemented using OOUI widgets.
Compact stacked vertical format for forms.
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:328