MediaWiki  master
HTMLCheckField.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
16  public function getInputHTML( $value ) {
17  $useMediaWikiUIEverywhere = false;
18  if ( $this->mParent ) {
19  $useMediaWikiUIEverywhere = $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
20  }
21 
22  if ( !empty( $this->mParams['invert'] ) ) {
23  $value = !$value;
24  }
25 
26  $attr = $this->getTooltipAndAccessKey();
27  $attr['id'] = $this->mID;
28 
29  $attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
30 
31  if ( $this->mClass !== '' ) {
32  $attr['class'] = $this->mClass;
33  }
34 
35  $attrLabel = [ 'for' => $this->mID ];
36  if ( isset( $attr['title'] ) ) {
37  // propagate tooltip to label
38  $attrLabel['title'] = $attr['title'];
39  }
40 
41  $chkLabel = Xml::check( $this->mName, $value, $attr ) .
42  "\u{00A0}" .
43  Html::rawElement( 'label', $attrLabel, $this->mLabel );
44 
45  if ( $useMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
46  $chkLabel = Html::rawElement(
47  'div',
48  [ 'class' => 'mw-ui-checkbox' ],
49  $chkLabel
50  );
51  }
52 
53  return $chkLabel;
54  }
55 
63  public function getInputOOUI( $value ) {
64  if ( !empty( $this->mParams['invert'] ) ) {
65  $value = !$value;
66  }
67 
68  $attr = $this->getTooltipAndAccessKeyOOUI();
69  $attr['id'] = $this->mID;
70  $attr['name'] = $this->mName;
71 
72  $attr += OOUI\Element::configFromHtmlAttributes(
73  $this->getAttributes( [ 'disabled', 'tabindex' ] )
74  );
75 
76  if ( $this->mClass !== '' ) {
77  $attr['classes'] = [ $this->mClass ];
78  }
79 
80  $attr['selected'] = $value;
81  $attr['value'] = '1'; // Nasty hack, but needed to make this work
82 
83  return new OOUI\CheckboxInputWidget( $attr );
84  }
85 
96  public function getLabel() {
97  if ( $this->mParent instanceof OOUIHTMLForm ) {
98  return $this->mLabel;
99  } elseif (
100  $this->mParent instanceof HTMLForm &&
101  $this->mParent->getDisplayFormat() === 'div'
102  ) {
103  return '';
104  } else {
105  return "\u{00A0}";
106  }
107  }
108 
114  protected function getLabelAlignOOUI() {
115  return 'inline';
116  }
117 
123  protected function needsLabel() {
124  return false;
125  }
126 
133  public function loadDataFromRequest( $request ) {
134  $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
135 
136  // Fetch the value in either one of the two following case:
137  // - we have a valid submit attempt (form was just submitted)
138  // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
139  if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
140  return $invert
141  ? !$request->getBool( $this->mName )
142  : $request->getBool( $this->mName );
143  } else {
144  return (bool)$this->getDefault();
145  }
146  }
147 }
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:150
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:500
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
A class containing constants representing the names of configuration variables.
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:332