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 ] + \OOUI\Element::configFromHtmlAttributes(
119 $this->getAttributes( [ 'disabled', 'tabindex' ] )
120 ) );
121 }
122
124 public function getInputCodex( $value, $hasErrors ) {
125 $flags = $this->mFlags;
126 $buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() );
127 $buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ];
128 $buttonAttribs = [
129 'class' => $buttonClasses,
130 'id' => $this->mID,
131 'type' => $this->buttonType,
132 'name' => $this->mName,
133 'value' => $this->getDefault(),
134 'formnovalidate' => $this->mFormnovalidate,
135 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
136
137 return static::buildCodexComponent(
138 $flags,
140 $buttonAttribs
141 );
142 }
143
153 public static function buildCodexComponent(
154 $flags,
156 $attribs
157 ) {
158 $flagClasses = [];
159 $flagClassMap = [
160 'progressive' => 'cdx-button--action-progressive',
161 'destructive' => 'cdx-button--action-destructive',
162 'primary' => 'cdx-button--weight-primary',
163 'quiet' => 'cdx-button--weight-quiet',
164 ];
165
166 foreach ( $flags as $flag ) {
167 if ( isset( $flagClassMap[$flag] ) ) {
168 $flagClasses[] = $flagClassMap[$flag];
169 }
170 }
171
172 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
173 $attribs['class'] = $buttonClassesAndFlags;
174
175 $buttonHtml = Html::rawElement(
176 'button', $attribs, $buttonLabel
177 );
178
179 return $buttonHtml;
180 }
181
186 protected function needsLabel() {
187 return false;
188 }
189
199 public function validate( $value, $alldata ) {
200 return true;
201 }
202}
203
205class_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