MediaWiki REL1_35
HTMLUsersMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
19 public function loadDataFromRequest( $request ) {
20 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
21
22 $usersArray = explode( "\n", $value );
23 // Remove empty lines
24 $usersArray = array_values( array_filter( $usersArray, function ( $username ) {
25 return trim( $username ) !== '';
26 } ) );
27 // This function is expected to return a string
28 return implode( "\n", $usersArray );
29 }
30
31 public function validate( $value, $alldata ) {
32 if ( !$this->mParams['exists'] ) {
33 return true;
34 }
35
36 if ( $value === null ) {
37 return false;
38 }
39
40 // $value is a string, because HTMLForm fields store their values as strings
41 $usersArray = explode( "\n", $value );
42
43 if ( isset( $this->mParams['max'] ) && ( count( $usersArray ) > $this->mParams['max'] ) ) {
44 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
45 }
46
47 foreach ( $usersArray as $username ) {
48 $result = parent::validate( $username, $alldata );
49 if ( $result !== true ) {
50 return $result;
51 }
52 }
53
54 return true;
55 }
56
57 public function getInputHTML( $value ) {
58 $this->mParent->getOutput()->enableOOUI();
59 return $this->getInputOOUI( $value );
60 }
61
62 public function getInputOOUI( $value ) {
63 $params = [ 'name' => $this->mName ];
64
65 if ( isset( $this->mParams['id'] ) ) {
66 $params['id'] = $this->mParams['id'];
67 }
68
69 if ( isset( $this->mParams['disabled'] ) ) {
70 $params['disabled'] = $this->mParams['disabled'];
71 }
72
73 if ( isset( $this->mParams['default'] ) ) {
74 $params['default'] = $this->mParams['default'];
75 }
76
77 if ( isset( $this->mParams['placeholder'] ) ) {
78 $params['placeholder'] = $this->mParams['placeholder'];
79 } else {
80 $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
81 }
82
83 if ( isset( $this->mParams['max'] ) ) {
84 $params['tagLimit'] = $this->mParams['max'];
85 }
86
87 if ( isset( $this->mParams['ipallowed'] ) ) {
88 $params['ipAllowed'] = $this->mParams['ipallowed'];
89 }
90
91 if ( isset( $this->mParams['iprange'] ) ) {
92 $params['ipRangeAllowed'] = $this->mParams['iprange'];
93 }
94
95 if ( isset( $this->mParams['iprangelimits'] ) ) {
96 $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
97 }
98
99 if ( isset( $this->mParams['input'] ) ) {
100 $params['input'] = $this->mParams['input'];
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-field-autoinfuse' ];
111 $widget = new UsersMultiselectWidget( $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.UsersMultiselectWidget' ];
123 }
124
125}
msg( $key,... $params)
Get a translated interface message.
getDefault()
Stable to override.
Implements a text input field for user names.
Implements a tag multiselect input field for user names.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
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.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...