MediaWiki REL1_39
OOUIHTMLForm.php
Go to the documentation of this file.
1<?php
2
29class OOUIHTMLForm extends HTMLForm {
30 private $oouiErrors;
31 private $oouiWarnings;
32
37 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
38 parent::__construct( $descriptor, $context, $messagePrefix );
39 $this->getOutput()->enableOOUI();
40 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
41 }
42
47 protected $displayFormat = 'ooui';
48
49 public static function loadInputFromParameters( $fieldname, $descriptor,
50 HTMLForm $parent = null
51 ) {
52 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
53 $field->setShowEmptyLabel( false );
54 return $field;
55 }
56
57 public function getButtons() {
58 $buttons = '';
59
60 if ( $this->mShowSubmit ) {
61 $attribs = [ 'infusable' => true ];
62
63 if ( isset( $this->mSubmitID ) ) {
64 $attribs['id'] = $this->mSubmitID;
65 }
66
67 if ( isset( $this->mSubmitName ) ) {
68 $attribs['name'] = $this->mSubmitName;
69 }
70
71 if ( isset( $this->mSubmitTooltip ) ) {
72 $attribs += [
73 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
74 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
75 ];
76 }
77
78 $attribs['classes'] = [ 'mw-htmlform-submit' ];
79 $attribs['type'] = 'submit';
80 $attribs['label'] = $this->getSubmitText();
81 $attribs['value'] = $this->getSubmitText();
82 $attribs['flags'] = $this->mSubmitFlags;
83
84 $buttons .= new OOUI\ButtonInputWidget( $attribs );
85 }
86
87 if ( $this->mShowReset ) {
88 $buttons .= new OOUI\ButtonInputWidget( [
89 'type' => 'reset',
90 'label' => $this->msg( 'htmlform-reset' )->text(),
91 ] );
92 }
93
94 if ( $this->mShowCancel ) {
95 $target = $this->getCancelTargetURL();
96 $buttons .= new OOUI\ButtonWidget( [
97 'label' => $this->msg( 'cancel' )->text(),
98 'href' => $target,
99 ] );
100 }
101
102 foreach ( $this->mButtons as $button ) {
103 $attrs = [];
104
105 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
106 if ( $button['attribs'] ) {
107 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
108 $attrs += $button['attribs'];
109 }
110
111 if ( isset( $button['id'] ) ) {
112 $attrs['id'] = $button['id'];
113 }
114
115 if ( isset( $button['label-message'] ) ) {
116 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
117 } elseif ( isset( $button['label'] ) ) {
118 $label = $button['label'];
119 } elseif ( isset( $button['label-raw'] ) ) {
120 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
121 } else {
122 $label = $button['value'];
123 }
124
125 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
126
127 $buttons .= new OOUI\ButtonInputWidget( [
128 'type' => 'submit',
129 'name' => $button['name'],
130 'value' => $button['value'],
131 'label' => $label,
132 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
133 'flags' => $button['flags'],
134 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in HTMLForm::addButton
135 'framed' => $button['framed'],
136 ] + $attrs );
137 }
138
139 if ( !$buttons ) {
140 return '';
141 }
142
143 return Html::rawElement( 'div',
144 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
145 }
146
151 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
152 // to get a user visible effect, wrap the fieldset into a framed panel layout
153 $layout = new OOUI\PanelLayout( [
154 'expanded' => false,
155 'padded' => true,
156 'framed' => true,
157 ] );
158
159 $layout->appendContent(
160 new OOUI\FieldsetLayout( [
161 'label' => $legend,
162 'items' => [
163 new OOUI\Widget( [
164 'content' => new OOUI\HtmlSnippet( $section )
165 ] ),
166 ],
167 ] + $attributes )
168 );
169 return $layout;
170 }
171
179 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
180 if ( !$fieldsHtml ) {
181 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
182 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
183 return '';
184 }
185
186 $html = implode( '', $fieldsHtml );
187
188 if ( $sectionName ) {
189 $html = Html::rawElement(
190 'div',
191 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
192 $html
193 );
194 }
195 return $html;
196 }
197
203 public function getErrorsOrWarnings( $elements, $elementsType ) {
204 if ( $elements === '' ) {
205 return '';
206 }
207
208 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
209 throw new DomainException( $elementsType . ' is not a valid type.' );
210 }
211 $errors = [];
212 if ( $elements instanceof Status ) {
213 if ( !$elements->isGood() ) {
214 $errors = $elements->getErrorsByType( $elementsType );
215 foreach ( $errors as &$error ) {
216 // Input: [ 'message' => 'foo', 'params' => [ 'a', 'b', 'c' ] ]
217 // Output: [ 'foo', 'a', 'b', 'c' ]
218 $error = $this->getMessage(
219 array_merge( [ $error['message'] ], $error['params'] ) )->parse();
220 }
221 }
222 } elseif ( $elementsType === 'error' ) {
223 if ( is_array( $elements ) ) {
224 foreach ( $elements as $error ) {
225 $errors[] = $this->getMessage( $error )->parse();
226 }
227 } elseif ( $elements && $elements !== true ) {
228 $errors[] = (string)$elements;
229 }
230 }
231
232 foreach ( $errors as &$error ) {
233 $error = new OOUI\HtmlSnippet( $error );
234 }
235
236 // Used in formatFormHeader()
237 if ( $elementsType === 'error' ) {
238 $this->oouiErrors = $errors;
239 } else {
240 $this->oouiWarnings = $errors;
241 }
242 return '';
243 }
244
245 public function getHeaderHtml( $section = null ) {
246 if ( $section === null ) {
247 // We handle $this->mHeader elsewhere, in getBody()
248 return '';
249 } else {
250 return parent::getHeaderHtml( $section );
251 }
252 }
253
254 protected function formatFormHeader() {
255 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
256 return '';
257 }
258 $classes = [ 'mw-htmlform-ooui-header' ];
259 if ( $this->oouiErrors ) {
260 $classes[] = 'mw-htmlform-ooui-header-errors';
261 }
262 if ( $this->oouiWarnings ) {
263 $classes[] = '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 $html = parent::getBody();
284 $html = $this->formatFormHeader() . $html;
285 return $html;
286 }
287
288 public function wrapForm( $html ) {
289 if ( is_string( $this->mWrapperLegend ) ) {
290 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class;
291 $content = new $phpClass( [
292 'label' => $this->mWrapperLegend,
293 'collapsed' => $this->mCollapsed,
294 'items' => [
295 new OOUI\Widget( [
296 'content' => new OOUI\HtmlSnippet( $html )
297 ] ),
298 ],
299 ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
300 } else {
301 $content = new OOUI\HtmlSnippet( $html );
302 }
303
304 $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ];
305 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
306 'classes' => $classes,
307 'content' => $content,
308 ] );
309
310 // Include a wrapper for style, if requested.
311 $form = new OOUI\PanelLayout( [
312 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
313 'expanded' => false,
314 'padded' => $this->mWrapperLegend !== false,
315 'framed' => $this->mWrapperLegend !== false,
316 'content' => $form,
317 ] );
318
319 return $form;
320 }
321}
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:150
string[] $mSubmitFlags
Definition HTMLForm.php:211
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.
static accesskey( $name, $localizer=null)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition Linker.php:2128
static titleAttrib( $name, $options=null, array $msgParams=[], $localizer=null)
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
Definition Linker.php:2080
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:44
$content
Definition router.php:76
return true
Definition router.php:92