MediaWiki master
HTMLButtonField.php
Go to the documentation of this file.
1<?php
2
4
8use Message;
9
31 protected $buttonType = 'button';
32 protected $buttonLabel = null;
33
35 protected $mFlags = [];
36
37 protected $mFormnovalidate = false;
38
43 public function __construct( $info ) {
44 $info['nodata'] = true;
45
46 $this->setShowEmptyLabel( false );
47
48 parent::__construct( $info );
49
50 if ( isset( $info['flags'] ) ) {
51 $this->mFlags = $info['flags'];
52 }
53
54 if ( isset( $info['formnovalidate'] ) ) {
55 $this->mFormnovalidate = $info['formnovalidate'];
56 }
57
58 # Generate the label from a message, if possible
59 if ( isset( $info['buttonlabel-message'] ) ) {
60 $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
61 } elseif ( isset( $info['buttonlabel'] ) ) {
62 if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
63 // Apparently some things set &nbsp directly and in an odd format
64 $this->buttonLabel = "\u{00A0}";
65 } else {
66 $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
67 }
68 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
69 $this->buttonLabel = $info['buttonlabel-raw'];
70 }
71 }
72
73 public function getInputHTML( $value ) {
74 $flags = '';
75 $prefix = 'mw-htmlform-';
76 if ( $this->mParent instanceof VFormHTMLForm ) {
77 $prefix = 'mw-ui-';
78 // add mw-ui-button separately, so the descriptor doesn't need to set it
79 $flags .= ' ' . $prefix . 'button';
80 }
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 ] + \OOUI\Element::configFromHtmlAttributes(
115 $this->getAttributes( [ 'disabled', 'tabindex' ] )
116 ) );
117 }
118
119 public function getInputCodex( $value, $hasErrors ) {
120 $flags = $this->mFlags;
121 $buttonLabel = $this->buttonLabel ?: htmlspecialchars( $this->getDefault() );
122 $buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ];
123 $buttonAttribs = [
124 'class' => $buttonClasses,
125 'id' => $this->mID,
126 'type' => $this->buttonType,
127 'name' => $this->mName,
128 'value' => $this->getDefault(),
129 'formnovalidate' => $this->mFormnovalidate,
130 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
131
132 return static::buildCodexComponent(
133 $flags,
135 $buttonAttribs
136 );
137 }
138
147 public static function buildCodexComponent(
148 $flags,
150 $attribs
151 ) {
152 $flagClasses = [];
153 $flagClassMap = [
154 'progressive' => 'cdx-button--action-progressive',
155 'destructive' => 'cdx-button--action-destructive',
156 'primary' => 'cdx-button--weight-primary',
157 'quiet' => 'cdx-button--weight-quiet',
158 ];
159
160 foreach ( $flags as $flag ) {
161 if ( isset( $flagClassMap[$flag] ) ) {
162 $flagClasses[] = $flagClassMap[$flag];
163 }
164 }
165
166 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
167 $attribs['class'] = $buttonClassesAndFlags;
168
169 $buttonHtml = Html::rawElement(
170 'button', $attribs, $buttonLabel
171 );
172
173 return $buttonHtml;
174 }
175
180 protected function needsLabel() {
181 return false;
182 }
183
193 public function validate( $value, $alldata ) {
194 return true;
195 }
196}
197
199class_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