MediaWiki master
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->mShowCancel ) {
93 $buttons .= new \OOUI\ButtonWidget( [
94 'label' => $this->msg( 'cancel' )->text(),
95 'href' => $this->getCancelTargetURL(),
96 ] );
97 }
98
99 foreach ( $this->mButtons as $button ) {
100 $attrs = [];
101
102 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
103 if ( $button['attribs'] ) {
104 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
105 $attrs += $button['attribs'];
106 }
107
108 if ( isset( $button['id'] ) ) {
109 $attrs['id'] = $button['id'];
110 }
111
112 if ( isset( $button['label-message'] ) ) {
113 $label = new \OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
114 } elseif ( isset( $button['label'] ) ) {
115 $label = $button['label'];
116 } elseif ( isset( $button['label-raw'] ) ) {
117 $label = new \OOUI\HtmlSnippet( $button['label-raw'] );
118 } else {
119 $label = $button['value'];
120 }
121
122 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
123
124 $buttons .= new \OOUI\ButtonInputWidget( [
125 'type' => 'submit',
126 'name' => $button['name'],
127 'value' => $button['value'],
128 'label' => $label,
129 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
130 'flags' => $button['flags'],
131 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
132 'framed' => $button['framed'],
133 ] + $attrs );
134 }
135
136 if ( !$buttons ) {
137 return '';
138 }
139
140 return Html::rawElement( 'div',
141 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
142 }
143
148 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
149 // to get a user visible effect, wrap the fieldset into a framed panel layout
150 $layout = new \OOUI\PanelLayout( [
151 'expanded' => false,
152 'padded' => true,
153 'framed' => true,
154 ] );
155
156 $layout->appendContent(
157 new \OOUI\FieldsetLayout( [
158 'label' => $legend,
159 'items' => [
160 new \OOUI\Widget( [
161 'content' => new \OOUI\HtmlSnippet( $section )
162 ] ),
163 ],
164 ] + $attributes )
165 );
166 return $layout;
167 }
168
173 protected function formatField( HTMLFormField $field, $value ) {
174 return $field->getOOUI( $value );
175 }
176
184 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
185 if ( !$fieldsHtml ) {
186 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
187 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
188 return '';
189 }
190
191 $html = implode( '', $fieldsHtml );
192
193 if ( $sectionName ) {
194 return Html::rawElement(
195 'div',
196 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
197 $html
198 );
199 }
200 return $html;
201 }
202
208 public function getErrorsOrWarnings( $elements, $elementsType ) {
209 if ( $elements === '' ) {
210 return '';
211 }
212
213 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
214 throw new DomainException( $elementsType . ' is not a valid type.' );
215 }
216 $errors = [];
217 if ( $elements instanceof Status ) {
218 if ( !$elements->isGood() ) {
219 $errors = $elements->getErrorsByType( $elementsType );
220 foreach ( $errors as &$error ) {
221 $error = $this->getMessage( [ $error['message'], ...$error['params'] ] )->parse();
222 }
223 }
224 } elseif ( $elementsType === 'error' ) {
225 if ( is_array( $elements ) ) {
226 foreach ( $elements as $error ) {
227 $errors[] = $this->getMessage( $error )->parse();
228 }
229 } elseif ( $elements && $elements !== true ) {
230 $errors[] = (string)$elements;
231 }
232 }
233
234 foreach ( $errors as &$error ) {
235 $error = new \OOUI\HtmlSnippet( $error );
236 }
237
238 // Used in formatFormHeader()
239 if ( $elementsType === 'error' ) {
240 $this->oouiErrors = $errors;
241 } else {
242 $this->oouiWarnings = $errors;
243 }
244 return '';
245 }
246
247 public function getHeaderHtml( $section = null ) {
248 if ( $section === null ) {
249 // We handle $this->mHeader elsewhere, in getBody()
250 return '';
251 } else {
252 return parent::getHeaderHtml( $section );
253 }
254 }
255
256 protected function formatFormHeader() {
257 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
258 return '';
259 }
260 $classes = [
261 'mw-htmlform-ooui-header',
262 ...$this->oouiErrors ? [ 'mw-htmlform-ooui-header-errors' ] : [],
263 ...$this->oouiWarnings ? [ 'mw-htmlform-ooui-header-warnings' ] : [],
264 ];
265 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
266 if ( $this->mHeader ) {
267 $element = new \OOUI\LabelWidget( [ 'label' => new \OOUI\HtmlSnippet( $this->mHeader ) ] );
268 } else {
269 $element = new \OOUI\Widget( [] );
270 }
271 return new \OOUI\FieldLayout(
272 $element,
273 [
274 'align' => 'top',
275 'errors' => $this->oouiErrors,
276 'notices' => $this->oouiWarnings,
277 'classes' => $classes,
278 ]
279 );
280 }
281
282 public function getBody() {
283 return $this->formatFormHeader() . parent::getBody();
284 }
285
286 public function wrapForm( $html ) {
287 if ( is_string( $this->mWrapperLegend ) ) {
288 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : \OOUI\FieldsetLayout::class;
289 $content = new $phpClass( [
290 'label' => $this->mWrapperLegend,
291 'collapsed' => $this->mCollapsed,
292 'items' => [
293 new \OOUI\Widget( [
294 'content' => new \OOUI\HtmlSnippet( $html )
295 ] ),
296 ],
297 ] + \OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
298 } else {
299 $content = new \OOUI\HtmlSnippet( $html );
300 }
301
302 $form = new \OOUI\FormLayout( $this->getFormAttributes() + [
303 'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
304 'content' => $content,
305 ] );
306
307 // Include a wrapper for style, if requested.
308 $form = new \OOUI\PanelLayout( [
309 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
310 'expanded' => false,
311 'padded' => $this->mWrapperLegend !== false,
312 'framed' => $this->mWrapperLegend !== false,
313 'content' => $form,
314 ] );
315
316 return $form;
317 }
318}
319
321class_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