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
39 public function loadDataFromRequest( $request ) {
40 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
41
42 $titlesArray = explode( "\n", $value );
43 // Remove empty lines
44 $titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) {
45 return trim( $title ) !== '';
46 } ) );
47 // This function is expected to return a string
48 return implode( "\n", $titlesArray );
49 }
50
51 public function validate( $value, $alldata ) {
52 if ( !$this->mParams['exists'] ) {
53 return true;
54 }
55
56 if ( $value === null ) {
57 return false;
58 }
59
60 // $value is a string, because HTMLForm fields store their values as strings
61 $titlesArray = explode( "\n", $value );
62
63 if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
64 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
65 }
66
67 foreach ( $titlesArray as $title ) {
68 $result = parent::validate( $title, $alldata );
69 if ( $result !== true ) {
70 return $result;
71 }
72 }
73
74 return true;
75 }
76
77 public function getInputHTML( $value ) {
78 $this->mParent->getOutput()->enableOOUI();
79 return $this->getInputOOUI( $value );
80 }
81
82 public function getInputOOUI( $value ) {
83 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
84
85 $params = [
86 'id' => $this->mID,
87 'name' => $this->mName,
88 'dir' => $this->mDir,
89 ];
90
91 if ( isset( $this->mParams['disabled'] ) ) {
92 $params['disabled'] = $this->mParams['disabled'];
93 }
94
95 if ( isset( $this->mParams['default'] ) ) {
96 $params['default'] = $this->mParams['default'];
97 }
98
99 $params['placeholder'] = $this->mParams['placeholder'] ??
100 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
101
102 if ( isset( $this->mParams['max'] ) ) {
103 $params['tagLimit'] = $this->mParams['max'];
104 }
105
106 if ( isset( $this->mParams['showMissing'] ) ) {
107 $params['showMissing'] = $this->mParams['showMissing'];
108 }
109 if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
110 $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
111 }
112 if ( isset( $this->mParams['allowEditTags'] ) ) {
113 $params['allowEditTags'] = $this->mParams['allowEditTags'];
114 }
115
116 if ( isset( $this->mParams['input'] ) ) {
117 $params['input'] = $this->mParams['input'];
118 }
119
120 if ( $value !== null ) {
121 // $value is a string, but the widget expects an array
122 $params['default'] = $value === '' ? [] : explode( "\n", $value );
123 }
124
125 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
126 $params['infusable'] = true;
127 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
128
129 return $this->getInputWidget( $params );
130 }
131
135 protected function getInputWidget( $params ) {
136 $widget = new TitlesMultiselectWidget( $params );
137 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
138 return $widget;
139 }
140
141 protected function shouldInfuseOOUI() {
142 return true;
143 }
144
145 protected function getOOUIModules() {
146 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
147 }
148
149 public function getInputCodex( $value, $hasErrors ) {
150 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
151 // Use 10 instead (but allow $this->mParams to override that value)
152 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
153 return $textAreaField->getInputCodex( $value, $hasErrors );
154 }
155
156}
157
159class_alias( HTMLTitlesMultiselectField::class, 'HTMLTitlesMultiselectField' );
array $params
The job parameters.
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.
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.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
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.