MediaWiki master
HTMLTitlesMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
6
30 public function __construct( $params ) {
31 $params += [
32 // This overrides the default from HTMLTitleTextField
33 'required' => false,
34 ];
35
36 parent::__construct( $params );
37 }
38
40 public function loadDataFromRequest( $request ) {
41 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
42
43 $titlesArray = explode( "\n", $value );
44 // Remove empty lines
45 $titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) {
46 return trim( $title ) !== '';
47 } ) );
48 // This function is expected to return a string
49 return implode( "\n", $titlesArray );
50 }
51
53 public function validate( $value, $alldata ) {
54 if ( !$this->mParams['exists'] ) {
55 return true;
56 }
57
58 if ( $value === null ) {
59 return false;
60 }
61
62 // $value is a string, because HTMLForm fields store their values as strings
63 $titlesArray = explode( "\n", $value );
64
65 if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
66 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
67 }
68
69 foreach ( $titlesArray as $title ) {
70 $result = parent::validate( $title, $alldata );
71 if ( $result !== true ) {
72 return $result;
73 }
74 }
75
76 return true;
77 }
78
80 public function getInputHTML( $value ) {
81 $this->mParent->getOutput()->enableOOUI();
82 return $this->getInputOOUI( $value );
83 }
84
86 public function getInputOOUI( $value ) {
87 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
88
89 $params = [
90 'id' => $this->mID,
91 'name' => $this->mName,
92 'dir' => $this->mDir,
93 ];
94
95 if ( isset( $this->mParams['disabled'] ) ) {
96 $params['disabled'] = $this->mParams['disabled'];
97 }
98
99 if ( isset( $this->mParams['default'] ) ) {
100 $params['default'] = $this->mParams['default'];
101 }
102
103 $params['placeholder'] = $this->mParams['placeholder'] ??
104 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
105
106 if ( isset( $this->mParams['max'] ) ) {
107 $params['tagLimit'] = $this->mParams['max'];
108 }
109
110 if ( isset( $this->mParams['showMissing'] ) ) {
111 $params['showMissing'] = $this->mParams['showMissing'];
112 }
113 if ( $this->mParams['namespace'] !== false ) {
114 $params['namespace'] = $this->mParams['namespace'];
115 }
116 $params['relative'] = $this->mParams['relative'];
117 if ( isset( $this->mParams['allowEditTags'] ) ) {
118 $params['allowEditTags'] = $this->mParams['allowEditTags'];
119 }
120
121 if ( isset( $this->mParams['input'] ) ) {
122 $params['input'] = $this->mParams['input'];
123 }
124
125 if ( isset( $this->mParams['creatable'] ) ) {
126 $params['creatable'] = $this->mParams['creatable'];
127 }
128
129 if ( $value !== null ) {
130 // $value is a string, but the widget expects an array
131 $params['default'] = $value === '' ? [] : explode( "\n", $value );
132 }
133
134 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
135 $params['infusable'] = true;
136 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
137
138 return $this->getInputWidget( $params );
139 }
140
144 protected function getInputWidget( $params ) {
145 $widget = new TitlesMultiselectWidget( $params );
146 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
147 return $widget;
148 }
149
151 protected function shouldInfuseOOUI() {
152 return true;
153 }
154
156 protected function getOOUIModules() {
157 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
158 }
159
161 public function getInputCodex( $value, $hasErrors ) {
162 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
163 // Use 10 instead (but allow $this->mParams to override that value)
164 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
165 return $textAreaField->getInputCodex( $value, $hasErrors );
166 }
167
168}
169
171class_alias( HTMLTitlesMultiselectField::class, 'HTMLTitlesMultiselectField' );
Implements a text input field for page titles.
Implements a tag multiselect input field for titles.
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
msg( $key,... $params)
Get a translated interface message.