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