MediaWiki master
CodexHTMLForm.php
Go to the documentation of this file.
1<?php
2
11
16
22class CodexHTMLForm extends HTMLForm {
23
25 protected $displayFormat = 'codex';
26
28 public static function loadInputFromParameters( $fieldname, $descriptor,
29 ?HTMLForm $parent = null
30 ) {
31 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
32 $field->setShowEmptyLabel( false );
33 return $field;
34 }
35
37 public function getHTML( $submitResult ) {
38 $this->getOutput()->addModuleStyles( [
39 'mediawiki.htmlform.codex.styles',
40 ] );
41
42 return parent::getHTML( $submitResult );
43 }
44
48 protected function formatField( HTMLFormField $field, $value ) {
49 return $field->getCodex( $value );
50 }
51
53 protected function getFormAttributes() {
54 $attribs = parent::getFormAttributes();
55 $attribs['class'] = [ 'mw-htmlform', 'mw-htmlform-codex' ];
56 return $attribs;
57 }
58
60 public function wrapForm( $html ) {
61 return Html::rawElement( 'form', $this->getFormAttributes(), $html );
62 }
63
65 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
66 $attributes['class'] = 'cdx-field cdx-field--is-fieldset';
67 $legendElement = Html::rawElement( 'legend', [ 'class' => [ 'cdx-label' ] ], $legend );
68 return Html::rawElement( 'fieldset', $attributes, "$legendElement\n$section" ) . "\n";
69 }
70
78 public function getLegend( $key ) {
79 $legendText = $this->msg(
80 $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key
81 )->text();
82 $legendTextMarkup = Html::element(
83 'span',
84 [ 'class' => [ 'cdx-label__label__text' ] ],
85 $legendText
86 );
87
88 $isOptional = $this->mSections[$key]['optional'] ?? false;
89 $optionalFlagMarkup = '';
90 if ( $isOptional ) {
91 $optionalFlagMarkup = Html::element(
92 'span',
93 [ 'class' => [ 'cdx-label__label__optional-flag' ] ],
94 $this->msg( 'word-separator' )->text() . $this->msg( 'htmlform-optional-flag' )->text()
95 );
96 }
97
98 $descriptionMarkup = '';
99 if ( isset( $this->mSections[$key]['description-message'] ) ) {
100 $needsParse = $this->mSections[ $key ][ 'description-message-parse' ] ?? false;
101 $descriptionMessage = $this->msg( $this->mSections[ $key ][ 'description-message' ] );
102 $descriptionMarkup = Html::rawElement(
103 'span',
104 [ 'class' => [ 'cdx-label__description' ] ],
105 $needsParse ? $descriptionMessage->parse() : $descriptionMessage->escaped()
106 );
107 } elseif ( isset( $this->mSections[$key]['description'] ) ) {
108 $descriptionMarkup = Html::element(
109 'span',
110 [ 'class' => [ 'cdx-label__description' ] ],
111 $this->mSections[ $key ][ 'description' ]
112 );
113 }
114
115 return Html::rawElement(
116 'span',
117 [ 'class' => [ 'cdx-label__label' ] ],
118 $legendTextMarkup . $optionalFlagMarkup
119 ) . $descriptionMarkup;
120 }
121
123 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
124 if ( !$fieldsHtml ) {
125 // Do not generate any wrappers for empty sections. Sections may be empty if they only
126 // have subsections, but no fields. A legend will still be added in
127 // wrapFieldSetSection().
128 return '';
129 }
130
131 $html = implode( '', $fieldsHtml );
132
133 if ( $sectionName ) {
134 $attribs = [
135 'id' => Sanitizer::escapeIdForAttribute( $sectionName ),
136 'class' => [ 'cdx-field__control' ]
137 ];
138
139 return Html::rawElement( 'div', $attribs, $html );
140 }
141
142 return $html;
143 }
144
150 public function getButtons() {
151 $buttons = [];
152
153 if ( $this->mShowSubmit ) {
154 $value = $this->getSubmitText();
155 // Define flag classes for the submit button
156 $submitFlags = $this->mSubmitFlags;
157 $submitClasses = [ 'mw-htmlform-submit', 'cdx-button' ];
158 $submitButtonLabel = $this->getSubmitText();
159 $submitID = $this->mSubmitID;
160 $submitName = $this->mSubmitName;
161 $submitTooltip = [];
162
163 if ( $this->mSubmitTooltip !== null ) {
164 $submitTooltip += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
165 }
166
167 $buttonAttribs = [
168 'value' => $value,
169 'type' => 'submit',
170 'name' => $submitName,
171 'id' => $submitID,
172 'class' => $submitClasses,
173 'formnovalidate' => false,
174 ] + $submitTooltip;
175
177 $submitFlags,
178 htmlspecialchars( $submitButtonLabel ),
179 $buttonAttribs
180 );
181 $buttons[] = $button;
182 }
183
184 // The reset button is unused and will be removed from HTMLForm (T361032).
185
186 if ( $this->mShowCancel ) {
187 $target = $this->getCancelTargetURL();
188 $buttonClasses = [
189 'cdx-button',
190 'cdx-button--fake-button',
191 'cdx-button--fake-button--enabled',
192 ];
193 $attr = [
194 'href' => $target,
195 'class' => $buttonClasses,
196 'role' => 'button',
197 ];
198 $cancelButton = Html::element(
199 'a', $attr, $this->msg( 'cancel' )->text()
200 );
201 $buttons[] = $cancelButton;
202 }
203
204 foreach ( $this->mButtons as $button ) {
205 $attrs = [
206 'type' => 'submit',
207 'name' => $button['name'],
208 'value' => $button['value']
209 ];
210
211 if ( isset( $button['label-message'] ) ) {
212 $label = $this->getMessage( $button['label-message'] )->parse();
213 } elseif ( isset( $button['label'] ) ) {
214 $label = htmlspecialchars( $button['label'] );
215 } elseif ( isset( $button['label-raw'] ) ) {
216 $label = $button['label-raw'];
217 } else {
218 $label = htmlspecialchars( $button['value'] );
219 }
220
221 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
222 if ( $button['attribs'] ) {
223 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
224 $attrs += $button['attribs'];
225 }
226
227 if ( isset( $button['id'] ) ) {
228 $attrs['id'] = $button['id'];
229 }
230
231 Html::addClass( $attrs['class'], 'cdx-button' );
232
233 $buttons[] = Html::rawElement( 'button', $attrs, $label ) . "\n";
234 }
235
236 if ( !$buttons ) {
237 return '';
238 }
239
240 return Html::rawElement(
241 'div',
242 [ 'class' => 'mw-htmlform-submit-buttons' ],
243 implode( "\n", $buttons )
244 ) . "\n";
245 }
246}
247
249class_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.to override string|\OOUI\Tag Wrapped HTML.
getHTML( $submitResult)
Returns the raw HTML generated by the form.to overridestring HTML
static loadInputFromParameters( $fieldname, $descriptor, ?HTMLForm $parent=null)
Initialise a new Object for the field.to overrideNot passing (or passing null) for $parent is depreca...
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.to override array
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.to overridestring The fieldset's Html
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:195
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:43
Some internal bits split of from Skin.php.
Definition Linker.php:47
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:32
element(SerializerNode $parent, SerializerNode $node, $contents)