MediaWiki master
CodexHTMLForm.php
Go to the documentation of this file.
1<?php
2
25
30
36class CodexHTMLForm extends HTMLForm {
37
38 protected $displayFormat = 'codex';
39
40 public static function loadInputFromParameters( $fieldname, $descriptor,
41 HTMLForm $parent = null
42 ) {
43 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
44 $field->setShowEmptyLabel( false );
45 return $field;
46 }
47
48 public function getHTML( $submitResult ) {
49 $this->getOutput()->addModuleStyles( [
50 'mediawiki.htmlform.codex.styles',
51 ] );
52
53 return parent::getHTML( $submitResult );
54 }
55
59 protected function formatField( HTMLFormField $field, $value ) {
60 return $field->getCodex( $value );
61 }
62
63 protected function getFormAttributes() {
64 $attribs = parent::getFormAttributes();
65 $attribs['class'] = [ 'mw-htmlform', 'mw-htmlform-codex' ];
66 return $attribs;
67 }
68
69 public function wrapForm( $html ) {
70 return Html::rawElement( 'form', $this->getFormAttributes(), $html );
71 }
72
73 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
74 $attributes['class'] = 'cdx-field';
75 $legendElement = Html::rawElement( 'legend', [ 'class' => [ 'cdx-label' ] ], $legend );
76 return Html::rawElement( 'fieldset', $attributes, "$legendElement\n$section" ) . "\n";
77 }
78
86 public function getLegend( $key ) {
87 $legendText = $this->msg(
88 $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key
89 )->text();
90 $legendTextMarkup = Html::element(
91 'span',
92 [ 'class' => [ 'cdx-label__label__text' ] ],
93 $legendText
94 );
95
96 $isOptional = $this->mSections[$key]['optional'] ?? false;
97 $optionalFlagMarkup = '';
98 if ( $isOptional ) {
99 $optionalFlagMarkup = Html::element(
100 'span',
101 [ 'class' => [ 'cdx-label__label__optional-flag' ] ],
102 $this->msg( 'word-separator' )->text() . $this->msg( 'htmlform-optional-flag' )->text()
103 );
104 }
105
106 $descriptionMarkup = '';
107 if ( isset( $this->mSections[$key]['description-message'] ) ) {
108 $needsParse = $this->mSections[ $key ][ 'description-message-parse' ] ?? false;
109 $descriptionMessage = $this->msg( $this->mSections[ $key ][ 'description-message' ] );
110 $descriptionMarkup = Html::rawElement(
111 'span',
112 [ 'class' => [ 'cdx-label__description' ] ],
113 $needsParse ? $descriptionMessage->parse() : $descriptionMessage->escaped()
114 );
115 } elseif ( isset( $this->mSections[$key]['description'] ) ) {
116 $descriptionMarkup = Html::element(
117 'span',
118 [ 'class' => [ 'cdx-label__description' ] ],
119 $this->mSections[ $key ][ 'description' ]
120 );
121 }
122
123 return Html::rawElement(
124 'span',
125 [ 'class' => [ 'cdx-label__label' ] ],
126 $legendTextMarkup . $optionalFlagMarkup
127 ) . $descriptionMarkup;
128 }
129
130 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
131 if ( !$fieldsHtml ) {
132 // Do not generate any wrappers for empty sections. Sections may be empty if they only
133 // have subsections, but no fields. A legend will still be added in
134 // wrapFieldSetSection().
135 return '';
136 }
137
138 $html = implode( '', $fieldsHtml );
139
140 if ( $sectionName ) {
141 $attribs = [
142 'id' => Sanitizer::escapeIdForAttribute( $sectionName ),
143 'class' => [ 'cdx-field__control' ]
144 ];
145
146 return Html::rawElement( 'div', $attribs, $html );
147 }
148
149 return $html;
150 }
151
157 public function getButtons() {
158 $buttons = [];
159
160 if ( $this->mShowSubmit ) {
161 $value = $this->getSubmitText();
162 // Define flag classes for the submit button
163 $submitFlags = $this->mSubmitFlags;
164 $submitClasses = [ 'mw-htmlform-submit', 'cdx-button' ];
165 $submitButtonLabel = $this->getSubmitText();
166 $submitID = $this->mSubmitID;
167 $submitName = $this->mSubmitName;
168 $submitTooltip = [];
169
170 if ( isset( $this->mSubmitTooltip ) ) {
171 $submitTooltip += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
172 }
173
174 $buttonAttribs = [
175 'value' => $value,
176 'type' => 'submit',
177 'name' => $submitName,
178 'id' => $submitID,
179 'class' => $submitClasses,
180 'formnovalidate' => false,
181 ] + $submitTooltip;
182
184 $submitFlags,
185 $submitButtonLabel,
186 $buttonAttribs
187 );
188 $buttons[] = $button;
189 }
190
191 // The reset button is unused and will be removed from HTMLForm (T361032).
192
193 if ( $this->mShowCancel ) {
194 $target = $this->getCancelTargetURL();
195 $buttonClasses = [
196 'cdx-button',
197 'cdx-button--fake-button',
198 'cdx-button--fake-button--enabled',
199 ];
200 $attr = [
201 'href' => $target,
202 'class' => $buttonClasses,
203 'role' => 'button',
204 ];
205 $cancelButton = Html::element(
206 'a', $attr, $this->msg( 'cancel' )->text()
207 );
208 $buttons[] = $cancelButton;
209 }
210
211 foreach ( $this->mButtons as $button ) {
212 $attrs = [
213 'type' => 'submit',
214 'name' => $button['name'],
215 'value' => $button['value']
216 ];
217
218 if ( isset( $button['label-message'] ) ) {
219 $label = $this->getMessage( $button['label-message'] )->parse();
220 } elseif ( isset( $button['label'] ) ) {
221 $label = htmlspecialchars( $button['label'] );
222 } elseif ( isset( $button['label-raw'] ) ) {
223 $label = $button['label-raw'];
224 } else {
225 $label = htmlspecialchars( $button['value'] );
226 }
227
228 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
229 if ( $button['attribs'] ) {
230 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
231 $attrs += $button['attribs'];
232 }
233
234 if ( isset( $button['id'] ) ) {
235 $attrs['id'] = $button['id'];
236 }
237
238 if ( isset( $attrs['class'] ) ) {
239 // Ensure $attrs['class'] is always treated as an array whether it's initially set
240 // as an array or a string.
241 $attrs['class'] = (array)( $attrs['class'] ?? [] );
242 }
243
244 $attrs['class'][] = 'cdx-button';
245
246 $buttons[] = Html::rawElement( 'button', $attrs, $label ) . "\n";
247 }
248
249 if ( !$buttons ) {
250 return '';
251 }
252
253 return Html::rawElement(
254 'div',
255 [ 'class' => 'mw-htmlform-submit-buttons' ],
256 implode( "\n", $buttons )
257 ) . "\n";
258 }
259}
260
262class_alias( CodexHTMLForm::class, 'CodexHTMLForm' );
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
getHTML( $submitResult)
Returns the raw HTML generated by the form.
getButtons()
Get the submit and cancel buttons.
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
getLegend( $key)
Note that this method returns HTML, while the parent method specifies that it should return a plain s...
getFormAttributes()
Get HTML attributes for the <form> tag.
formatField(HTMLFormField $field, $value)
Generate the HTML for an individual field in the current display format.1.41 to override string|Strin...
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into a user-visible fieldset.
Adds a generic button inline to the form.
static buildCodexComponent( $flags, $buttonLabel, $attribs)
Build the markup of the Codex component.
The parent class to generate form fields.
getCodex( $value)
Get the Codex version of the div.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getSubmitText()
Get the text for the submit button, either customised or a default.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Some internal bits split of from Skin.php.
Definition Linker.php:63
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
element(SerializerNode $parent, SerializerNode $node, $contents)