Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLButtonField.php
blob1a9340c2bb6fa0acd21a14145d75373c99cf8cbc
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7 use MediaWiki\HTMLForm\VFormHTMLForm;
8 use MediaWiki\Message\Message;
10 /**
11 * Adds a generic button inline to the form. Does not do anything, you must add
12 * click handling code in JavaScript. Use a HTMLSubmitField if you merely
13 * wish to add a submit button to a form.
15 * Additional recognized configuration parameters include:
16 * - flags: OOUI flags for the button, see OOUI\FlaggedElement
17 * - buttonlabel-message: Message to use for the button display text, instead
18 * of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
19 * - buttonlabel: Text to display for the button display text, instead
20 * of the value from 'default'. Overrides 'buttonlabel-raw'.
21 * - buttonlabel-raw: HTMLto display for the button display text, instead
22 * of the value from 'default'.
23 * - formnovalidate: Set to true if clicking this button should suppress
24 * client-side form validation. Used in HTMLFormFieldCloner for add/remove
25 * buttons.
27 * @stable to extend
28 * @since 1.22
30 class HTMLButtonField extends HTMLFormField {
31 /** @var string */
32 protected $buttonType = 'button';
33 /** @var string|null */
34 protected $buttonLabel = null;
36 /** @var array Flags to add to OOUI Button widget */
37 protected $mFlags = [];
39 /** @var bool */
40 protected $mFormnovalidate = false;
42 /**
43 * @stable to call
44 * @inheritDoc
46 public function __construct( $info ) {
47 $info['nodata'] = true;
49 $this->setShowEmptyLabel( false );
51 parent::__construct( $info );
53 if ( isset( $info['flags'] ) ) {
54 $this->mFlags = $info['flags'];
57 if ( isset( $info['formnovalidate'] ) ) {
58 $this->mFormnovalidate = $info['formnovalidate'];
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'] );
71 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
72 $this->buttonLabel = $info['buttonlabel-raw'];
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';
84 foreach ( $this->mFlags as $flag ) {
85 $flags .= ' ' . $prefix . $flag;
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' ] );
96 return Html::rawElement( 'button', $attr,
97 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
101 * Get the OOUI widget for this field.
102 * @stable to override
103 * @param string $value
104 * @return \OOUI\ButtonInputWidget
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 ) );
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' ] );
135 return static::buildCodexComponent(
136 $flags,
137 $buttonLabel,
138 $buttonAttribs
143 * Build the markup of the Codex component
145 * @param array $flags The button's flag classes.
146 * @param string $buttonLabel The button's label attribute.
147 * @param array $attribs The button's list of attributes.
148 * @return string Raw HTML.
150 public static function buildCodexComponent(
151 $flags,
152 $buttonLabel,
153 $attribs
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',
163 foreach ( $flags as $flag ) {
164 if ( isset( $flagClassMap[$flag] ) ) {
165 $flagClasses[] = $flagClassMap[$flag];
169 $buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
170 $attribs['class'] = $buttonClassesAndFlags;
172 $buttonHtml = Html::rawElement(
173 'button', $attribs, $buttonLabel
176 return $buttonHtml;
180 * @inheritDoc
181 * @stable to override
183 protected function needsLabel() {
184 return false;
188 * Button cannot be invalid
189 * @stable to override
191 * @param string $value
192 * @param array $alldata
194 * @return bool|string|Message
196 public function validate( $value, $alldata ) {
197 return true;
201 /** @deprecated class alias since 1.42 */
202 class_alias( HTMLButtonField::class, 'HTMLButtonField' );