MediaWiki master
HTMLNamespacesMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
7
21 public function loadDataFromRequest( $request ) {
22 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
23
24 $namespaces = explode( "\n", $value );
25 // Remove empty lines
26 $namespaces = array_values( array_filter( $namespaces, static function ( $namespace ) {
27 return trim( $namespace ) !== '';
28 } ) );
29 // This function is expected to return a string
30 return implode( "\n", $namespaces );
31 }
32
33 public function validate( $value, $alldata ) {
34 if ( !$this->mParams['exists'] || $value === '' ) {
35 return true;
36 }
37
38 if ( $value === null ) {
39 return false;
40 }
41
42 // $value is a string, because HTMLForm fields store their values as strings
43 $namespaces = explode( "\n", $value );
44
45 if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) {
46 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
47 }
48
49 foreach ( $namespaces as $namespace ) {
50 if (
51 $namespace < 0 ||
52 !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( (int)$namespace )
53 ) {
54 return $this->msg( 'htmlform-select-badoption' );
55 }
56
57 $result = parent::validate( $namespace, $alldata );
58 if ( $result !== true ) {
59 return $result;
60 }
61 }
62
63 return true;
64 }
65
66 public function getInputHTML( $value ) {
67 $this->mParent->getOutput()->enableOOUI();
68 return $this->getInputOOUI( $value );
69 }
70
71 public function getInputOOUI( $value ) {
72 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
73
74 $params = [
75 'id' => $this->mID,
76 'name' => $this->mName,
77 'dir' => $this->mDir,
78 ];
79
80 if ( isset( $this->mParams['disabled'] ) ) {
81 $params['disabled'] = $this->mParams['disabled'];
82 }
83
84 if ( isset( $this->mParams['default'] ) ) {
85 $params['default'] = $this->mParams['default'];
86 }
87
88 $params['placeholder'] = $this->mParams['placeholder'] ??
89 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
90
91 if ( isset( $this->mParams['max'] ) ) {
92 $params['tagLimit'] = $this->mParams['max'];
93 }
94
95 if ( isset( $this->mParams['input'] ) ) {
96 $params['input'] = $this->mParams['input'];
97 }
98
99 if ( isset( $this->mParams['allowEditTags'] ) ) {
100 $params['allowEditTags'] = $this->mParams['allowEditTags'];
101 }
102
103 if ( $value !== null ) {
104 // $value is a string, but the widget expects an array
105 $params['default'] = $value === '' ? [] : explode( "\n", $value );
106 }
107
108 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
109 $params['infusable'] = true;
110 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
111 $widget = new NamespacesMultiselectWidget( $params );
112 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
113
114 return $widget;
115 }
116
117 protected function shouldInfuseOOUI() {
118 return true;
119 }
120
121 protected function getOOUIModules() {
122 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
123 }
124
125}
126
128class_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....
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
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.