MediaWiki master
HTMLButtonField.php
Go to the documentation of this file.
1<?php
2
4
9
32 protected $buttonType = 'button';
34 protected $buttonLabel = null;
35
37 protected $mFlags = [];
38
40 protected $mFormnovalidate = false;
41
46 public function __construct( $info ) {
47 $info['nodata'] = true;
48
49 $this->setShowEmptyLabel( false );
50
51 parent::__construct( $info );
52
53 if ( isset( $info['flags'] ) ) {
54 $this->mFlags = $info['flags'];
55 }
56
57 if ( isset( $info['formnovalidate'] ) ) {
58 $this->mFormnovalidate = $info['formnovalidate'];
59 }
60
61 # Generate the label from a message, if possible
62 if ( isset( $info['buttonlabel-message'] ) ) {
63 $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
64 } elseif ( isset( $info['buttonlabel'] ) ) {
65 if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
66 // Apparently some things set &nbsp directly and in an odd format
67 $this->buttonLabel = "\u{00A0}";
68 } else {
69 $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
70 }
71 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
72 $this->buttonLabel = $info['buttonlabel-raw'];
73 }
74 }
75
77 public function getInputHTML( $value ) {
78 $flags = '';
79 $prefix = 'mw-htmlform-';
80 if ( $this->mParent instanceof VFormHTMLForm ) {
81 $prefix = 'mw-ui-';
82 // add mw-ui-button separately, so the descriptor doesn't need to set it
83 $flags .= ' ' . $prefix . 'button';
84 }
85 foreach ( $this->mFlags as $flag ) {
86 $flags .= ' ' . $prefix . $flag;
87 }
88 $attr = [
89 'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
90 'id' => $this->mID,
91 'type' => $this->buttonType,
92 'name' => $this->mName,
93 'value' => $this->getDefault(),
94 'formnovalidate' => $this->mFormnovalidate,
95 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
96
97 return Html::rawElement( 'button', $attr,
98 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
99 }
100
107 public function getInputOOUI( $value ) {
108 return new \OOUI\ButtonInputWidget( [
109 'name' => $this->mName,
110 'value' => $this->getDefault(),
111 'label' => $this->buttonLabel
112 ? new \OOUI\HtmlSnippet( $this->buttonLabel )
113 : $this->getDefault(),
114 'type' => $this->buttonType,
115 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
116 'id' => $this->mID,
117 'flags' => $this->mFlags,
118 'formNoValidate' => $this->mFormnovalidate,
119 ] + \OOUI\Element::configFromHtmlAttributes(
120 $this->getAttributes( [ 'disabled', 'tabindex' ] )
121 ) );
122 }
123
125 public function getInputCodex( $value, $hasErrors ) {
126 $flags = $this->mFlags;
127 $buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() );
128 $buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ];
129 $buttonAttribs = [
130 'class' => $buttonClasses,
131 'id' => $this->mID,
132 'type' => $this->buttonType,
133 'name' => $this->mName,
134 'value' => $this->getDefault(),
135 'formnovalidate' => $this->mFormnovalidate,
136 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
137
138 return static::buildCodexComponent(
139 $flags,
141 $buttonAttribs
142 );
143 }
144
154 public static function buildCodexComponent(
155 $flags,
157 $attribs
158 ) {
159 $flagClasses = [];
160 $flagClassMap = [
161 'progressive' => 'cdx-button--action-progressive',
162 'destructive' => 'cdx-button--action-destructive',
163 'primary' => 'cdx-button--weight-primary',
164 'quiet' => 'cdx-button--weight-quiet',
165 ];
166
167 foreach ( $flags as $flag ) {
168 if ( isset( $flagClassMap[$flag] ) ) {
169 $flagClasses[] = $flagClassMap[$flag];
170 }
171 }
172
173 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
174 $attribs['class'] = $buttonClassesAndFlags;
175
176 $buttonHtml = Html::rawElement(
177 'button', $attribs, $buttonLabel
178 );
179
180 return $buttonHtml;
181 }
182
187 protected function needsLabel() {
188 return false;
189 }
190
200 public function validate( $value, $alldata ) {
201 return true;
202 }
203}
204
206class_alias( HTMLButtonField::class, 'HTMLButtonField' );
Adds a generic button inline to the form.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
validate( $value, $alldata)
Button cannot be invalid.
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
static buildCodexComponent( $flags, $buttonLabel, $attribs)
Build the markup of the Codex component.
getInputOOUI( $value)
Get the OOUI widget for this field.
array $mFlags
Flags to add to OOUI Button widget.
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getAttributes(array $list)
Returns the given attributes from the parameters.
setShowEmptyLabel( $show)
Tell the field whether to generate a separate label element if its label is blank.
Compact stacked vertical format for forms.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144