MediaWiki master
HTMLButtonField.php
Go to the documentation of this file.
1<?php
2
4
10use Message;
11
33 protected $buttonType = 'button';
34 protected $buttonLabel = null;
35
37 protected $mFlags = [];
38
39 protected $mFormnovalidate = false;
40
45 public function __construct( $info ) {
46 $info['nodata'] = true;
47
48 $this->setShowEmptyLabel( false );
49
50 parent::__construct( $info );
51
52 if ( isset( $info['flags'] ) ) {
53 $this->mFlags = $info['flags'];
54 }
55
56 if ( isset( $info['formnovalidate'] ) ) {
57 $this->mFormnovalidate = $info['formnovalidate'];
58 }
59
60 # Generate the label from a message, if possible
61 if ( isset( $info['buttonlabel-message'] ) ) {
62 $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
63 } elseif ( isset( $info['buttonlabel'] ) ) {
64 if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
65 // Apparently some things set &nbsp directly and in an odd format
66 $this->buttonLabel = "\u{00A0}";
67 } else {
68 $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
69 }
70 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
71 $this->buttonLabel = $info['buttonlabel-raw'];
72 }
73 }
74
75 public function getInputHTML( $value ) {
76 $flags = '';
77 $prefix = 'mw-htmlform-';
78 if ( $this->mParent instanceof VFormHTMLForm ) {
79 $prefix = 'mw-ui-';
80 // add mw-ui-button separately, so the descriptor doesn't need to set it
81 $flags .= ' ' . $prefix . 'button';
82 } elseif ( $this->mParent instanceof CodexHTMLForm ) {
83 $flags .= ' cdx-button cdx-button--action-progressive cdx-button--weight-primary';
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 if ( $this->isBadIE() ) {
98 return Html::element( 'input', $attr );
99 } else {
100 return Html::rawElement( 'button', $attr,
101 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
102 }
103 }
104
111 public function getInputOOUI( $value ) {
112 return new \OOUI\ButtonInputWidget( [
113 'name' => $this->mName,
114 'value' => $this->getDefault(),
115 'label' => !$this->isBadIE() && $this->buttonLabel
116 ? new \OOUI\HtmlSnippet( $this->buttonLabel )
117 : $this->getDefault(),
118 'type' => $this->buttonType,
119 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
120 'id' => $this->mID,
121 'flags' => $this->mFlags,
122 'useInputTag' => $this->isBadIE(),
123 ] + \OOUI\Element::configFromHtmlAttributes(
124 $this->getAttributes( [ 'disabled', 'tabindex' ] )
125 ) );
126 }
127
132 protected function needsLabel() {
133 return false;
134 }
135
145 public function validate( $value, $alldata ) {
146 return true;
147 }
148
153 private function isBadIE() {
154 $request = $this->mParent
155 ? $this->mParent->getRequest()
156 : RequestContext::getMain()->getRequest();
157 return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
158 }
159}
160
162class_alias( HTMLButtonField::class, 'HTMLButtonField' );
Group all the pieces relevant to the context of a request into one instance.
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...
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
element(SerializerNode $parent, SerializerNode $node, $contents)