MediaWiki REL1_41
OOUIHTMLForm.php
Go to the documentation of this file.
1<?php
2
28
34class OOUIHTMLForm extends HTMLForm {
35 private $oouiErrors;
36 private $oouiWarnings;
37
42 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
43 parent::__construct( $descriptor, $context, $messagePrefix );
44 $this->getOutput()->enableOOUI();
45 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
46 }
47
48 protected $displayFormat = 'ooui';
49
50 public static function loadInputFromParameters( $fieldname, $descriptor,
51 HTMLForm $parent = null
52 ) {
53 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
54 $field->setShowEmptyLabel( false );
55 return $field;
56 }
57
58 public function getButtons() {
59 $buttons = '';
60
61 if ( $this->mShowSubmit ) {
62 $attribs = [
63 'infusable' => true,
64 'classes' => [ 'mw-htmlform-submit' ],
65 'type' => 'submit',
66 'label' => $this->getSubmitText(),
67 'value' => $this->getSubmitText(),
68 'flags' => $this->mSubmitFlags,
69 ];
70
71 if ( isset( $this->mSubmitID ) ) {
72 $attribs['id'] = $this->mSubmitID;
73 }
74
75 if ( isset( $this->mSubmitName ) ) {
76 $attribs['name'] = $this->mSubmitName;
77 }
78
79 if ( isset( $this->mSubmitTooltip ) ) {
80 $attribs += [
81 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
82 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
83 ];
84 }
85
86 $buttons .= new OOUI\ButtonInputWidget( $attribs );
87 }
88
89 if ( $this->mShowReset ) {
90 $buttons .= new OOUI\ButtonInputWidget( [
91 'type' => 'reset',
92 'label' => $this->msg( 'htmlform-reset' )->text(),
93 ] );
94 }
95
96 if ( $this->mShowCancel ) {
97 $buttons .= new OOUI\ButtonWidget( [
98 'label' => $this->msg( 'cancel' )->text(),
99 'href' => $this->getCancelTargetURL(),
100 ] );
101 }
102
103 foreach ( $this->mButtons as $button ) {
104 $attrs = [];
105
106 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
107 if ( $button['attribs'] ) {
108 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
109 $attrs += $button['attribs'];
110 }
111
112 if ( isset( $button['id'] ) ) {
113 $attrs['id'] = $button['id'];
114 }
115
116 if ( isset( $button['label-message'] ) ) {
117 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
118 } elseif ( isset( $button['label'] ) ) {
119 $label = $button['label'];
120 } elseif ( isset( $button['label-raw'] ) ) {
121 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
122 } else {
123 $label = $button['value'];
124 }
125
126 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
127
128 $buttons .= new OOUI\ButtonInputWidget( [
129 'type' => 'submit',
130 'name' => $button['name'],
131 'value' => $button['value'],
132 'label' => $label,
133 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
134 'flags' => $button['flags'],
135 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
136 'framed' => $button['framed'],
137 ] + $attrs );
138 }
139
140 if ( !$buttons ) {
141 return '';
142 }
143
144 return Html::rawElement( 'div',
145 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
146 }
147
152 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
153 // to get a user visible effect, wrap the fieldset into a framed panel layout
154 $layout = new OOUI\PanelLayout( [
155 'expanded' => false,
156 'padded' => true,
157 'framed' => true,
158 ] );
159
160 $layout->appendContent(
161 new OOUI\FieldsetLayout( [
162 'label' => $legend,
163 'items' => [
164 new OOUI\Widget( [
165 'content' => new OOUI\HtmlSnippet( $section )
166 ] ),
167 ],
168 ] + $attributes )
169 );
170 return $layout;
171 }
172
177 protected function formatField( HTMLFormField $field, $value ) {
178 return $field->getOOUI( $value );
179 }
180
188 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
189 if ( !$fieldsHtml ) {
190 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
191 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
192 return '';
193 }
194
195 $html = implode( '', $fieldsHtml );
196
197 if ( $sectionName ) {
198 return Html::rawElement(
199 'div',
200 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
201 $html
202 );
203 }
204 return $html;
205 }
206
212 public function getErrorsOrWarnings( $elements, $elementsType ) {
213 if ( $elements === '' ) {
214 return '';
215 }
216
217 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
218 throw new DomainException( $elementsType . ' is not a valid type.' );
219 }
220 $errors = [];
221 if ( $elements instanceof Status ) {
222 if ( !$elements->isGood() ) {
223 $errors = $elements->getErrorsByType( $elementsType );
224 foreach ( $errors as &$error ) {
225 // Input: [ 'message' => 'foo', 'params' => [ 'a', 'b', 'c' ] ]
226 // Output: [ 'foo', 'a', 'b', 'c' ]
227 $error = $this->getMessage(
228 array_merge( [ $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}
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:158
getSubmitText()
Get the text for the submit button, either customised or a default.
getCancelTargetURL()
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getFormAttributes()
Get HTML attributes for the <form> tag.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
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:58
Compact stacked vertical format for forms, implemented using OOUI widgets.
__construct( $descriptor, $context=null, $messagePrefix='')
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into a user-visible fieldset.to overridestring The fieldset's Html
getErrorsOrWarnings( $elements, $elementsType)
getHeaderHtml( $section=null)
Get header HTML.
getBody()
Get the whole body of the form.
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
getButtons()
Get the submit and (potentially) reset buttons.
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
formatField(HTMLFormField $field, $value)
Generate the HTML for an individual field in the current display format.1.41 to override string|Strin...
$content
Definition router.php:76