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