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