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 if ( $button['attribs'] ) {
94 $attrs += $button['attribs'];
95 }
96
97 if ( isset( $button['id'] ) ) {
98 $attrs['id'] = $button['id'];
99 }
100
101 if ( isset( $button['label-message'] ) ) {
102 $label = new \OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
103 } elseif ( isset( $button['label'] ) ) {
104 $label = $button['label'];
105 } elseif ( isset( $button['label-raw'] ) ) {
106 $label = new \OOUI\HtmlSnippet( $button['label-raw'] );
107 } else {
108 $label = $button['value'];
109 }
110
111 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
112
113 $buttons .= new \OOUI\ButtonInputWidget( [
114 'type' => 'submit',
115 'name' => $button['name'],
116 'value' => $button['value'],
117 'label' => $label,
118 'flags' => $button['flags'],
119 'framed' => $button['framed'],
120 ] + $attrs );
121 }
122
123 if ( !$buttons ) {
124 return '';
125 }
126
127 return Html::rawElement( 'div',
128 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
129 }
130
135 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
136 // @phan-suppress-previous-line PhanParamSignatureMismatch We're different from our parent intentionally
137 // to get a user visible effect, wrap the fieldset into a framed panel layout
138 $layout = new \OOUI\PanelLayout( [
139 'expanded' => false,
140 'padded' => true,
141 'framed' => true,
142 ] );
143
144 $layout->appendContent(
145 new \OOUI\FieldsetLayout( [
146 'label' => $legend,
147 'items' => [
148 new \OOUI\Widget( [
149 'content' => new \OOUI\HtmlSnippet( $section )
150 ] ),
151 ],
152 ] + $attributes )
153 );
154 return $layout;
155 }
156
161 protected function formatField( HTMLFormField $field, $value ) {
162 return $field->getOOUI( $value );
163 }
164
172 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
173 if ( !$fieldsHtml ) {
174 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
175 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
176 return '';
177 }
178
179 $html = implode( '', $fieldsHtml );
180
181 if ( $sectionName ) {
182 return Html::rawElement(
183 'div',
184 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
185 $html
186 );
187 }
188 return $html;
189 }
190
196 public function getErrorsOrWarnings( $elements, $elementsType ) {
197 if ( $elements === '' ) {
198 return '';
199 }
200
201 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
202 throw new DomainException( $elementsType . ' is not a valid type.' );
203 }
204 $errors = [];
205 if ( $elements instanceof Status ) {
206 if ( !$elements->isGood() ) {
207 foreach ( $elements->getMessages( $elementsType ) as $msg ) {
208 $errors[] = $this->getMessage( $msg )->parse();
209 }
210 }
211 } elseif ( $elementsType === 'error' ) {
212 if ( is_array( $elements ) ) {
213 foreach ( $elements as $error ) {
214 $errors[] = $this->getMessage( $error )->parse();
215 }
216 } elseif ( $elements && $elements !== true ) {
217 $errors[] = (string)$elements;
218 }
219 }
220
221 foreach ( $errors as &$error ) {
222 $error = new \OOUI\HtmlSnippet( $error );
223 }
224
225 // Used in formatFormHeader()
226 if ( $elementsType === 'error' ) {
227 $this->oouiErrors = $errors;
228 } else {
229 $this->oouiWarnings = $errors;
230 }
231 return '';
232 }
233
235 public function getHeaderHtml( $section = null ) {
236 if ( $section === null ) {
237 // We handle $this->mHeader elsewhere, in getBody()
238 return '';
239 } else {
240 return parent::getHeaderHtml( $section );
241 }
242 }
243
244 protected function formatFormHeader(): string {
245 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
246 return '';
247 }
248 $classes = [
249 'mw-htmlform-ooui-header',
250 ...$this->oouiErrors ? [ 'mw-htmlform-ooui-header-errors' ] : [],
251 ...$this->oouiWarnings ? [ 'mw-htmlform-ooui-header-warnings' ] : [],
252 ];
253 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
254 if ( $this->mHeader ) {
255 $element = new \OOUI\LabelWidget( [ 'label' => new \OOUI\HtmlSnippet( $this->mHeader ) ] );
256 } else {
257 $element = new \OOUI\Widget( [] );
258 }
259 return ( new \OOUI\FieldLayout(
260 $element,
261 [
262 'align' => 'top',
263 'errors' => $this->oouiErrors,
264 'notices' => $this->oouiWarnings,
265 'classes' => $classes,
266 ]
267 ) )->toString();
268 }
269
271 public function getBody() {
272 return $this->formatFormHeader() . parent::getBody();
273 }
274
276 public function wrapForm( $html ) {
277 if ( is_string( $this->mWrapperLegend ) ) {
278 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : \OOUI\FieldsetLayout::class;
279 $content = new $phpClass( [
280 'label' => $this->mWrapperLegend,
281 'collapsed' => $this->mCollapsed,
282 'items' => [
283 new \OOUI\Widget( [
284 'content' => new \OOUI\HtmlSnippet( $html )
285 ] ),
286 ],
287 ] + \OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
288 } else {
289 $content = new \OOUI\HtmlSnippet( $html );
290 }
291
292 $form = new \OOUI\FormLayout( $this->getFormAttributes() + [
293 'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
294 'content' => $content,
295 ] );
296
297 // Include a wrapper for style, if requested.
298 $form = new \OOUI\PanelLayout( [
299 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
300 'expanded' => false,
301 'padded' => $this->mWrapperLegend !== false,
302 'framed' => $this->mWrapperLegend !== false,
303 'content' => $form,
304 ] );
305
306 return $form;
307 }
308}
309
311class_alias( OOUIHTMLForm::class, 'OOUIHTMLForm' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
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:208
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)
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:44
Some internal bits split of from Skin.php.
Definition Linker.php:47
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:34
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44