MediaWiki REL1_40
HTMLTitlesMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
28 public function __construct( $params ) {
29 $params += [
30 // This overrides the default from HTMLTitleTextField
31 'required' => false,
32 ];
33
34 parent::__construct( $params );
35 }
36
37 public function loadDataFromRequest( $request ) {
38 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
39
40 $titlesArray = explode( "\n", $value );
41 // Remove empty lines
42 $titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) {
43 return trim( $title ) !== '';
44 } ) );
45 // This function is expected to return a string
46 return implode( "\n", $titlesArray );
47 }
48
49 public function validate( $value, $alldata ) {
50 if ( !$this->mParams['exists'] ) {
51 return true;
52 }
53
54 if ( $value === null ) {
55 return false;
56 }
57
58 // $value is a string, because HTMLForm fields store their values as strings
59 $titlesArray = explode( "\n", $value );
60
61 if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
62 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
63 }
64
65 foreach ( $titlesArray as $title ) {
66 $result = parent::validate( $title, $alldata );
67 if ( $result !== true ) {
68 return $result;
69 }
70 }
71
72 return true;
73 }
74
75 public function getInputHTML( $value ) {
76 $this->mParent->getOutput()->enableOOUI();
77 return $this->getInputOOUI( $value );
78 }
79
80 public function getInputOOUI( $value ) {
81 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
82
83 $params = [
84 'id' => $this->mID,
85 'name' => $this->mName,
86 'dir' => $this->mDir,
87 ];
88
89 if ( isset( $this->mParams['disabled'] ) ) {
90 $params['disabled'] = $this->mParams['disabled'];
91 }
92
93 if ( isset( $this->mParams['default'] ) ) {
94 $params['default'] = $this->mParams['default'];
95 }
96
97 $params['placeholder'] = $this->mParams['placeholder'] ??
98 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
99
100 if ( isset( $this->mParams['max'] ) ) {
101 $params['tagLimit'] = $this->mParams['max'];
102 }
103
104 if ( isset( $this->mParams['showMissing'] ) ) {
105 $params['showMissing'] = $this->mParams['showMissing'];
106 }
107 if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
108 $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
109 }
110
111 if ( isset( $this->mParams['input'] ) ) {
112 $params['input'] = $this->mParams['input'];
113 }
114
115 if ( $value !== null ) {
116 // $value is a string, but the widget expects an array
117 $params['default'] = $value === '' ? [] : explode( "\n", $value );
118 }
119
120 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
121 $params['infusable'] = true;
122 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
123
124 return $this->getInputWidget( $params );
125 }
126
130 protected function getInputWidget( $params ) {
131 $widget = new TitlesMultiselectWidget( $params );
132 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
133 return $widget;
134 }
135
136 protected function shouldInfuseOOUI() {
137 return true;
138 }
139
140 protected function getOOUIModules() {
141 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
142 }
143
144}
msg( $key,... $params)
Get a translated interface message.
Implements a text input field for page titles.
Implements a tag multiselect input field for titles.
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 ...
shouldInfuseOOUI()
Whether the field should be automatically infused.
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....
getInputWidget( $params)
to overrideWidget
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...