MediaWiki 1.42.0
OOUIHTMLForm.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\HTMLForm;
4
26use DomainException;
31
37class OOUIHTMLForm extends HTMLForm {
38 private $oouiErrors;
39 private $oouiWarnings;
40
45 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
46 parent::__construct( $descriptor, $context, $messagePrefix );
47 $this->getOutput()->enableOOUI();
48 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
49 }
50
51 protected $displayFormat = 'ooui';
52
53 public static function loadInputFromParameters( $fieldname, $descriptor,
54 HTMLForm $parent = null
55 ) {
56 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
57 $field->setShowEmptyLabel( false );
58 return $field;
59 }
60
61 public function getButtons() {
62 $buttons = '';
63
64 if ( $this->mShowSubmit ) {
65 $attribs = [
66 'infusable' => true,
67 'classes' => [ 'mw-htmlform-submit' ],
68 'type' => 'submit',
69 'label' => $this->getSubmitText(),
70 'value' => $this->getSubmitText(),
71 'flags' => $this->mSubmitFlags,
72 ];
73
74 if ( isset( $this->mSubmitID ) ) {
75 $attribs['id'] = $this->mSubmitID;
76 }
77
78 if ( isset( $this->mSubmitName ) ) {
79 $attribs['name'] = $this->mSubmitName;
80 }
81
82 if ( isset( $this->mSubmitTooltip ) ) {
83 $attribs += [
84 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
85 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
86 ];
87 }
88
89 $buttons .= new \OOUI\ButtonInputWidget( $attribs );
90 }
91
92 if ( $this->mShowReset ) {
93 $buttons .= new \OOUI\ButtonInputWidget( [
94 'type' => 'reset',
95 'label' => $this->msg( 'htmlform-reset' )->text(),
96 ] );
97 }
98
99 if ( $this->mShowCancel ) {
100 $buttons .= new \OOUI\ButtonWidget( [
101 'label' => $this->msg( 'cancel' )->text(),
102 'href' => $this->getCancelTargetURL(),
103 ] );
104 }
105
106 foreach ( $this->mButtons as $button ) {
107 $attrs = [];
108
109 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
110 if ( $button['attribs'] ) {
111 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
112 $attrs += $button['attribs'];
113 }
114
115 if ( isset( $button['id'] ) ) {
116 $attrs['id'] = $button['id'];
117 }
118
119 if ( isset( $button['label-message'] ) ) {
120 $label = new \OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
121 } elseif ( isset( $button['label'] ) ) {
122 $label = $button['label'];
123 } elseif ( isset( $button['label-raw'] ) ) {
124 $label = new \OOUI\HtmlSnippet( $button['label-raw'] );
125 } else {
126 $label = $button['value'];
127 }
128
129 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
130
131 $buttons .= new \OOUI\ButtonInputWidget( [
132 'type' => 'submit',
133 'name' => $button['name'],
134 'value' => $button['value'],
135 'label' => $label,
136 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
137 'flags' => $button['flags'],
138 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
139 'framed' => $button['framed'],
140 ] + $attrs );
141 }
142
143 if ( !$buttons ) {
144 return '';
145 }
146
147 return Html::rawElement( 'div',
148 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
149 }
150
155 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
156 // to get a user visible effect, wrap the fieldset into a framed panel layout
157 $layout = new \OOUI\PanelLayout( [
158 'expanded' => false,
159 'padded' => true,
160 'framed' => true,
161 ] );
162
163 $layout->appendContent(
164 new \OOUI\FieldsetLayout( [
165 'label' => $legend,
166 'items' => [
167 new \OOUI\Widget( [
168 'content' => new \OOUI\HtmlSnippet( $section )
169 ] ),
170 ],
171 ] + $attributes )
172 );
173 return $layout;
174 }
175
180 protected function formatField( HTMLFormField $field, $value ) {
181 return $field->getOOUI( $value );
182 }
183
191 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
192 if ( !$fieldsHtml ) {
193 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
194 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
195 return '';
196 }
197
198 $html = implode( '', $fieldsHtml );
199
200 if ( $sectionName ) {
201 return Html::rawElement(
202 'div',
203 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
204 $html
205 );
206 }
207 return $html;
208 }
209
215 public function getErrorsOrWarnings( $elements, $elementsType ) {
216 if ( $elements === '' ) {
217 return '';
218 }
219
220 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
221 throw new DomainException( $elementsType . ' is not a valid type.' );
222 }
223 $errors = [];
224 if ( $elements instanceof Status ) {
225 if ( !$elements->isGood() ) {
226 $errors = $elements->getErrorsByType( $elementsType );
227 foreach ( $errors as &$error ) {
228 $error = $this->getMessage( [ $error['message'], ...$error['params'] ] )->parse();
229 }
230 }
231 } elseif ( $elementsType === 'error' ) {
232 if ( is_array( $elements ) ) {
233 foreach ( $elements as $error ) {
234 $errors[] = $this->getMessage( $error )->parse();
235 }
236 } elseif ( $elements && $elements !== true ) {
237 $errors[] = (string)$elements;
238 }
239 }
240
241 foreach ( $errors as &$error ) {
242 $error = new \OOUI\HtmlSnippet( $error );
243 }
244
245 // Used in formatFormHeader()
246 if ( $elementsType === 'error' ) {
247 $this->oouiErrors = $errors;
248 } else {
249 $this->oouiWarnings = $errors;
250 }
251 return '';
252 }
253
254 public function getHeaderHtml( $section = null ) {
255 if ( $section === null ) {
256 // We handle $this->mHeader elsewhere, in getBody()
257 return '';
258 } else {
259 return parent::getHeaderHtml( $section );
260 }
261 }
262
263 protected function formatFormHeader() {
264 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
265 return '';
266 }
267 $classes = [
268 'mw-htmlform-ooui-header',
269 ...$this->oouiErrors ? [ 'mw-htmlform-ooui-header-errors' ] : [],
270 ...$this->oouiWarnings ? [ 'mw-htmlform-ooui-header-warnings' ] : [],
271 ];
272 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
273 if ( $this->mHeader ) {
274 $element = new \OOUI\LabelWidget( [ 'label' => new \OOUI\HtmlSnippet( $this->mHeader ) ] );
275 } else {
276 $element = new \OOUI\Widget( [] );
277 }
278 return new \OOUI\FieldLayout(
279 $element,
280 [
281 'align' => 'top',
282 'errors' => $this->oouiErrors,
283 'notices' => $this->oouiWarnings,
284 'classes' => $classes,
285 ]
286 );
287 }
288
289 public function getBody() {
290 return $this->formatFormHeader() . parent::getBody();
291 }
292
293 public function wrapForm( $html ) {
294 if ( is_string( $this->mWrapperLegend ) ) {
295 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : \OOUI\FieldsetLayout::class;
296 $content = new $phpClass( [
297 'label' => $this->mWrapperLegend,
298 'collapsed' => $this->mCollapsed,
299 'items' => [
300 new \OOUI\Widget( [
301 'content' => new \OOUI\HtmlSnippet( $html )
302 ] ),
303 ],
304 ] + \OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
305 } else {
306 $content = new \OOUI\HtmlSnippet( $html );
307 }
308
309 $form = new \OOUI\FormLayout( $this->getFormAttributes() + [
310 'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
311 'content' => $content,
312 ] );
313
314 // Include a wrapper for style, if requested.
315 $form = new \OOUI\PanelLayout( [
316 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
317 'expanded' => false,
318 'padded' => $this->mWrapperLegend !== false,
319 'framed' => $this->mWrapperLegend !== false,
320 'content' => $form,
321 ] );
322
323 return $form;
324 }
325}
326
328class_alias( OOUIHTMLForm::class, 'OOUIHTMLForm' );
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
The parent class to generate form fields.
getOOUI( $value)
Get the OOUI version of the div.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
getFormAttributes()
Get HTML attributes for the <form> tag.
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.
Compact stacked vertical format for forms, implemented using OOUI widgets.
getButtons()
Get the submit and (potentially) reset buttons.
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into a user-visible fieldset.to overridestring The fieldset's Html
getBody()
Get the whole body of the form.
__construct( $descriptor, $context=null, $messagePrefix='')
formatField(HTMLFormField $field, $value)
Generate the HTML for an individual field in the current display format.1.41 to override string|Strin...
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
getErrorsOrWarnings( $elements, $elementsType)
getHeaderHtml( $section=null)
Get header HTML.
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:65
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54