3 use MediaWiki\Widget\NamespacesMultiselectWidget
;
6 * Implements a tag multiselect input field for namespaces.
8 * The result is the array of namespaces
10 * TODO: This widget duplicates a lot from HTMLTitlesMultiselectField,
11 * which itself duplicates HTMLUsersMultiselectField. These classes
12 * should be refactored.
14 * @note This widget is not likely to remain functional in non-OOUI forms.
16 class HTMLNamespacesMultiselectField
extends HTMLSelectNamespace
{
17 public function loadDataFromRequest( $request ) {
18 $value = $request->getText( $this->mName
, $this->getDefault() );
20 $namespaces = explode( "\n", $value );
22 $namespaces = array_values( array_filter( $namespaces, function ( $namespace ) {
23 return trim( $namespace ) !== '';
25 // This function is expected to return a string
26 return implode( "\n", $namespaces );
29 public function validate( $value, $alldata ) {
30 if ( !$this->mParams
['exists'] ) {
34 if ( is_null( $value ) ) {
38 // $value is a string, because HTMLForm fields store their values as strings
39 $namespaces = explode( "\n", $value );
41 if ( isset( $this->mParams
['max'] ) ) {
42 if ( count( $namespaces ) > $this->mParams
['max'] ) {
43 return $this->msg( 'htmlform-int-toohigh', $this->mParams
['max'] );
47 foreach ( $namespaces as $namespace ) {
48 $result = parent
::validate( $namespace, $alldata );
49 if ( $result !== true ) {
57 public function getInputHTML( $value ) {
58 $this->mParent
->getOutput()->enableOOUI();
59 return $this->getInputOOUI( $value );
62 public function getInputOOUI( $value ) {
65 'name' => $this->mName
,
69 if ( isset( $this->mParams
['disabled'] ) ) {
70 $params['disabled'] = $this->mParams
['disabled'];
73 if ( isset( $this->mParams
['default'] ) ) {
74 $params['default'] = $this->mParams
['default'];
77 if ( isset( $this->mParams
['placeholder'] ) ) {
78 $params['placeholder'] = $this->mParams
['placeholder'];
80 $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
83 if ( isset( $this->mParams
['max'] ) ) {
84 $params['tagLimit'] = $this->mParams
['max'];
87 if ( isset( $this->mParams
['input'] ) ) {
88 $params['input'] = $this->mParams
['input'];
91 if ( !is_null( $value ) ) {
92 // $value is a string, but the widget expects an array
93 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
96 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
97 $params['infusable'] = true;
98 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
99 $widget = new NamespacesMultiselectWidget( $params );
100 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
105 protected function shouldInfuseOOUI() {
109 protected function getOOUIModules() {
110 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];