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
76 public function getInputHTML( $value ) {
77 $flags = '';
78 $prefix = 'mw-htmlform-';
79 if ( $this->mParent instanceof VFormHTMLForm ) {
80 $prefix = 'mw-ui-';
81 // add mw-ui-button separately, so the descriptor doesn't need to set it
82 $flags .= ' ' . $prefix . 'button';
83 }
84 foreach ( $this->mFlags as $flag ) {
85 $flags .= ' ' . $prefix . $flag;
86 }
87 $attr = [
88 'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
89 'id' => $this->mID,
90 'type' => $this->buttonType,
91 'name' => $this->mName,
92 'value' => $this->getDefault(),
93 'formnovalidate' => $this->mFormnovalidate,
94 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
95
96 return Html::rawElement( 'button', $attr,
97 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
98 }
99
106 public function getInputOOUI( $value ) {
107 return new \OOUI\ButtonInputWidget( [
108 'name' => $this->mName,
109 'value' => $this->getDefault(),
110 'label' => $this->buttonLabel
111 ? new \OOUI\HtmlSnippet( $this->buttonLabel )
112 : $this->getDefault(),
113 'type' => $this->buttonType,
114 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
115 'id' => $this->mID,
116 'flags' => $this->mFlags,
117 ] + \OOUI\Element::configFromHtmlAttributes(
118 $this->getAttributes( [ 'disabled', 'tabindex' ] )
119 ) );
120 }
121
122 public function getInputCodex( $value, $hasErrors ) {
123 $flags = $this->mFlags;
124 $buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() );
125 $buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ];
126 $buttonAttribs = [
127 'class' => $buttonClasses,
128 'id' => $this->mID,
129 'type' => $this->buttonType,
130 'name' => $this->mName,
131 'value' => $this->getDefault(),
132 'formnovalidate' => $this->mFormnovalidate,
133 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
134
135 return static::buildCodexComponent(
136 $flags,
138 $buttonAttribs
139 );
140 }
141
150 public static function buildCodexComponent(
151 $flags,
153 $attribs
154 ) {
155 $flagClasses = [];
156 $flagClassMap = [
157 'progressive' => 'cdx-button--action-progressive',
158 'destructive' => 'cdx-button--action-destructive',
159 'primary' => 'cdx-button--weight-primary',
160 'quiet' => 'cdx-button--weight-quiet',
161 ];
162
163 foreach ( $flags as $flag ) {
164 if ( isset( $flagClassMap[$flag] ) ) {
165 $flagClasses[] = $flagClassMap[$flag];
166 }
167 }
168
169 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
170 $attribs['class'] = $buttonClassesAndFlags;
171
172 $buttonHtml = Html::rawElement(
173 'button', $attribs, $buttonLabel
174 );
175
176 return $buttonHtml;
177 }
178
183 protected function needsLabel() {
184 return false;
185 }
186
196 public function validate( $value, $alldata ) {
197 return true;
198 }
199}
200
202class_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.
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:56
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155