MediaWiki  master
HTMLCheckField.php
Go to the documentation of this file.
1 <?php
2 
9 
14  public function getInputHTML( $value ) {
16 
17  if ( !empty( $this->mParams['invert'] ) ) {
18  $value = !$value;
19  }
20 
21  $attr = $this->getTooltipAndAccessKey();
22  $attr['id'] = $this->mID;
23 
24  $attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
25 
26  if ( $this->mClass !== '' ) {
27  $attr['class'] = $this->mClass;
28  }
29 
30  $attrLabel = [ 'for' => $this->mID ];
31  if ( isset( $attr['title'] ) ) {
32  // propagate tooltip to label
33  $attrLabel['title'] = $attr['title'];
34  }
35 
36  $chkLabel = Xml::check( $this->mName, $value, $attr ) .
37  "\u{00A0}" .
38  Html::rawElement( 'label', $attrLabel, $this->mLabel );
39 
40  if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
41  $chkLabel = Html::rawElement(
42  'div',
43  [ 'class' => 'mw-ui-checkbox' ],
44  $chkLabel
45  );
46  }
47 
48  return $chkLabel;
49  }
50 
58  public function getInputOOUI( $value ) {
59  if ( !empty( $this->mParams['invert'] ) ) {
60  $value = !$value;
61  }
62 
63  $attr = $this->getTooltipAndAccessKeyOOUI();
64  $attr['id'] = $this->mID;
65  $attr['name'] = $this->mName;
66 
67  $attr += OOUI\Element::configFromHtmlAttributes(
68  $this->getAttributes( [ 'disabled', 'tabindex' ] )
69  );
70 
71  if ( $this->mClass !== '' ) {
72  $attr['classes'] = [ $this->mClass ];
73  }
74 
75  $attr['selected'] = $value;
76  $attr['value'] = '1'; // Nasty hack, but needed to make this work
77 
78  return new OOUI\CheckboxInputWidget( $attr );
79  }
80 
91  public function getLabel() {
92  if ( $this->mParent instanceof OOUIHTMLForm ) {
93  return $this->mLabel;
94  } elseif (
95  $this->mParent instanceof HTMLForm &&
96  $this->mParent->getDisplayFormat() === 'div'
97  ) {
98  return '';
99  } else {
100  return "\u{00A0}";
101  }
102  }
103 
109  protected function getLabelAlignOOUI() {
110  return 'inline';
111  }
112 
118  protected function needsLabel() {
119  return false;
120  }
121 
128  public function loadDataFromRequest( $request ) {
129  $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
130 
131  // Fetch the value in either one of the two following case:
132  // - we have a valid submit attempt (form was just submitted)
133  // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
134  if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
135  return $invert
136  ? !$request->getBool( $this->mName )
137  : $request->getBool( $this->mName );
138  } else {
139  return (bool)$this->getDefault();
140  }
141  }
142 }
HTMLCheckField\getLabel
getLabel()
For a checkbox, the label goes on the right hand side, and is added in getInputHTML(),...
Definition: HTMLCheckField.php:91
HTMLFormField\getTooltipAndAccessKeyOOUI
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
Definition: HTMLFormField.php:1021
HTMLFormField\$mLabel
$mLabel
Definition: HTMLFormField.php:17
HTMLCheckField\getLabelAlignOOUI
getLabelAlignOOUI()
Get label alignment when generating field for OOUI.
Definition: HTMLCheckField.php:109
HTMLCheckField
A checkbox field.
Definition: HTMLCheckField.php:8
HTMLForm\getDisplayFormat
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:472
$wgUseMediaWikiUIEverywhere
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
Definition: DefaultSettings.php:3413
HTMLCheckField\getInputOOUI
getInputOOUI( $value)
Get the OOUI version of this field.
Definition: HTMLCheckField.php:58
VFormHTMLForm
Compact stacked vertical format for forms.
Definition: VFormHTMLForm.php:29
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:19
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:29
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:15
Xml\check
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:327
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:9
HTMLCheckField\loadDataFromRequest
loadDataFromRequest( $request)
Stable to override.
Definition: HTMLCheckField.php:128
HTMLCheckField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition: HTMLCheckField.php:14
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:18
HTMLFormField\getDefault
getDefault()
Stable to override.
Definition: HTMLFormField.php:999
HTMLCheckField\needsLabel
needsLabel()
checkboxes don't need a label.
Definition: HTMLCheckField.php:118
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
HTMLFormField\isSubmitAttempt
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
Definition: HTMLFormField.php:378
HTMLFormField\getTooltipAndAccessKey
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
Definition: HTMLFormField.php:1008
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
Definition: HTMLFormField.php:1039
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135