MediaWiki master
HTMLNamespacesMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
7
22 public function loadDataFromRequest( $request ) {
23 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
24
25 $namespaces = explode( "\n", $value );
26 // Remove empty lines
27 $namespaces = array_values( array_filter( $namespaces, static function ( $namespace ) {
28 return trim( $namespace ) !== '';
29 } ) );
30 // This function is expected to return a string
31 return implode( "\n", $namespaces );
32 }
33
35 public function validate( $value, $alldata ) {
36 if ( !$this->mParams['exists'] || $value === '' ) {
37 return true;
38 }
39
40 if ( $value === null ) {
41 return false;
42 }
43
44 // $value is a string, because HTMLForm fields store their values as strings
45 $namespaces = explode( "\n", $value );
46
47 if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) {
48 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
49 }
50
51 foreach ( $namespaces as $namespace ) {
52 if (
53 $namespace < 0 ||
54 !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( (int)$namespace )
55 ) {
56 return $this->msg( 'htmlform-select-badoption' );
57 }
58
59 $result = parent::validate( $namespace, $alldata );
60 if ( $result !== true ) {
61 return $result;
62 }
63 }
64
65 return true;
66 }
67
69 public function getInputHTML( $value ) {
70 $this->mParent->getOutput()->enableOOUI();
71 return $this->getInputOOUI( $value )->toString();
72 }
73
75 public function getInputOOUI( $value ) {
76 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
77
78 $params = [
79 'id' => $this->mID,
80 'name' => $this->mName,
81 'dir' => $this->mDir,
82 ];
83
84 if ( isset( $this->mParams['disabled'] ) ) {
85 $params['disabled'] = $this->mParams['disabled'];
86 }
87
88 if ( isset( $this->mParams['default'] ) ) {
89 $params['default'] = $this->mParams['default'];
90 }
91
92 $params['placeholder'] = $this->mParams['placeholder'] ??
93 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
94
95 if ( isset( $this->mParams['max'] ) ) {
96 $params['tagLimit'] = $this->mParams['max'];
97 }
98
99 if ( isset( $this->mParams['input'] ) ) {
100 $params['input'] = $this->mParams['input'];
101 }
102
103 if ( isset( $this->mParams['allowEditTags'] ) ) {
104 $params['allowEditTags'] = $this->mParams['allowEditTags'];
105 }
106
107 if ( $value !== null ) {
108 // $value is a string, but the widget expects an array
109 $params['default'] = $value === '' ? [] : explode( "\n", $value );
110 }
111
112 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
113 $params['infusable'] = true;
114 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
115 $widget = new NamespacesMultiselectWidget( $params );
116 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
117
118 return $widget;
119 }
120
122 protected function shouldInfuseOOUI() {
123 return true;
124 }
125
127 protected function getOOUIModules() {
128 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
129 }
130
132 public function getInputCodex( $value, $hasErrors ) {
133 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
134 // Use 10 instead (but allow $this->mParams to override that value)
135 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
136 return $textAreaField->getInputCodex( $value, $hasErrors );
137 }
138
139}
140
142class_alias( HTMLNamespacesMultiselectField::class, 'HTMLNamespacesMultiselectField' );
Implements a tag multiselect input field for namespaces.
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
Wrapper for Html::namespaceSelector to use in HTMLForm.
msg( $key,... $params)
Get a translated interface message.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.