MediaWiki master
HTMLButtonField.php
Go to the documentation of this file.
1<?php
2
4
8
33 protected $buttonType = 'button';
35 protected $buttonLabel = null;
36
38 protected $mFlags = [];
39
41 protected $mFormnovalidate = false;
42
47 public function __construct( $info ) {
48 $info['nodata'] = true;
49
50 $this->setShowEmptyLabel( false );
51
52 parent::__construct( $info );
53
54 if ( isset( $info['flags'] ) ) {
55 $this->mFlags = $info['flags'];
56 }
57
58 if ( isset( $info['formnovalidate'] ) ) {
59 $this->mFormnovalidate = $info['formnovalidate'];
60 }
61
62 # Generate the label from a message, if possible
63 if ( isset( $info['buttonlabel-message'] ) ) {
64 $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
65 } elseif ( isset( $info['buttonlabel'] ) ) {
66 if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
67 // Apparently some things set &nbsp directly and in an odd format
68 $this->buttonLabel = "\u{00A0}";
69 } else {
70 $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
71 }
72 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
73 $this->buttonLabel = $info['buttonlabel-raw'];
74 }
75 }
76
78 public function getInputHTML( $value ) {
79 $flags = '';
80 $prefix = 'mw-htmlform-';
81 foreach ( $this->mFlags as $flag ) {
82 $flags .= ' ' . $prefix . $flag;
83 }
84 $attr = [
85 'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
86 'id' => $this->mID,
87 'type' => $this->buttonType,
88 'name' => $this->mName,
89 'value' => $this->getDefault(),
90 'formnovalidate' => $this->mFormnovalidate,
91 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
92
93 return Html::rawElement( 'button', $attr,
94 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
95 }
96
103 public function getInputOOUI( $value ) {
104 return new \OOUI\ButtonInputWidget( [
105 'name' => $this->mName,
106 'value' => $this->getDefault(),
107 'label' => $this->buttonLabel
108 ? new \OOUI\HtmlSnippet( $this->buttonLabel )
109 : $this->getDefault(),
110 'type' => $this->buttonType,
111 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
112 'id' => $this->mID,
113 'flags' => $this->mFlags,
114 'formNoValidate' => $this->mFormnovalidate,
115 ] + \OOUI\Element::configFromHtmlAttributes(
116 $this->getAttributes( [ 'disabled', 'tabindex' ] )
117 ) );
118 }
119
121 public function getInputCodex( $value, $hasErrors ) {
122 $buttonClasses = [ 'mw-htmlform-submit', 'cdx-button' ];
123 if ( isset( $this->mParams['size'] ) ) {
124 $codexSizeKeywords = [ 'small', 'medium', 'large' ];
125 if ( !in_array( $this->mParams['size'], $codexSizeKeywords, true ) ) {
126 throw new \InvalidArgumentException(
127 'Invalid size for button: "' . $this->mParams['size'] . '". Please use one of the Codex keywords.'
128 );
129 }
130
131 $sizeClass = 'cdx-button--size-' . $this->mParams['size'];
132 $buttonClasses[] = $sizeClass;
133 }
134 $flags = $this->mFlags;
135 $buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() );
136 $buttonAttribs = [
137 'class' => $buttonClasses,
138 'id' => $this->mID,
139 'type' => $this->buttonType,
140 'name' => $this->mName,
141 'value' => $this->getDefault(),
142 'formnovalidate' => $this->mFormnovalidate,
143 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
144
145 return static::buildCodexComponent(
146 $flags,
148 $buttonAttribs
149 );
150 }
151
161 public static function buildCodexComponent(
162 $flags,
164 $attribs
165 ) {
166 $flagClasses = [];
167 $flagClassMap = [
168 'progressive' => 'cdx-button--action-progressive',
169 'destructive' => 'cdx-button--action-destructive',
170 'primary' => 'cdx-button--weight-primary',
171 'quiet' => 'cdx-button--weight-quiet',
172 ];
173
174 foreach ( $flags as $flag ) {
175 if ( isset( $flagClassMap[$flag] ) ) {
176 $flagClasses[] = $flagClassMap[$flag];
177 }
178 }
179
180 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
181 $attribs['class'] = $buttonClassesAndFlags;
182
183 $buttonHtml = Html::rawElement(
184 'button', $attribs, $buttonLabel
185 );
186
187 return $buttonHtml;
188 }
189
194 protected function needsLabel() {
195 return false;
196 }
197
207 public function validate( $value, $alldata ) {
208 return true;
209 }
210}
211
213class_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.
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144