MediaWiki  master
HTMLUsersMultiselectField.php
Go to the documentation of this file.
1 <?php
2 
4 
18  public function loadDataFromRequest( $request ) {
19  $value = $request->getText( $this->mName, $this->getDefault() );
20 
21  $usersArray = explode( "\n", $value );
22  // Remove empty lines
23  $usersArray = array_values( array_filter( $usersArray, function ( $username ) {
24  return trim( $username ) !== '';
25  } ) );
26  // This function is expected to return a string
27  return implode( "\n", $usersArray );
28  }
29 
30  public function validate( $value, $alldata ) {
31  if ( !$this->mParams['exists'] ) {
32  return true;
33  }
34 
35  if ( $value === null ) {
36  return false;
37  }
38 
39  // $value is a string, because HTMLForm fields store their values as strings
40  $usersArray = explode( "\n", $value );
41 
42  if ( isset( $this->mParams['max'] ) && ( count( $usersArray ) > $this->mParams['max'] ) ) {
43  return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
44  }
45 
46  foreach ( $usersArray as $username ) {
47  $result = parent::validate( $username, $alldata );
48  if ( $result !== true ) {
49  return $result;
50  }
51  }
52 
53  return true;
54  }
55 
56  public function getInputHTML( $value ) {
57  $this->mParent->getOutput()->enableOOUI();
58  return $this->getInputOOUI( $value );
59  }
60 
61  public function getInputOOUI( $value ) {
62  $params = [ 'name' => $this->mName ];
63 
64  if ( isset( $this->mParams['id'] ) ) {
65  $params['id'] = $this->mParams['id'];
66  }
67 
68  if ( isset( $this->mParams['disabled'] ) ) {
69  $params['disabled'] = $this->mParams['disabled'];
70  }
71 
72  if ( isset( $this->mParams['default'] ) ) {
73  $params['default'] = $this->mParams['default'];
74  }
75 
76  if ( isset( $this->mParams['placeholder'] ) ) {
77  $params['placeholder'] = $this->mParams['placeholder'];
78  } else {
79  $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
80  }
81 
82  if ( isset( $this->mParams['max'] ) ) {
83  $params['tagLimit'] = $this->mParams['max'];
84  }
85 
86  if ( isset( $this->mParams['ipallowed'] ) ) {
87  $params['ipAllowed'] = $this->mParams['ipallowed'];
88  }
89 
90  if ( isset( $this->mParams['iprange'] ) ) {
91  $params['ipRangeAllowed'] = $this->mParams['iprange'];
92  }
93 
94  if ( isset( $this->mParams['iprangelimits'] ) ) {
95  $params['ipRangeLimits'] = $this->mParams['iprangelimits'];
96  }
97 
98  if ( isset( $this->mParams['input'] ) ) {
99  $params['input'] = $this->mParams['input'];
100  }
101 
102  if ( $value !== null ) {
103  // $value is a string, but the widget expects an array
104  $params['default'] = $value === '' ? [] : explode( "\n", $value );
105  }
106 
107  // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
108  $params['infusable'] = true;
109  $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
110  $widget = new UsersMultiselectWidget( $params );
111  $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
112 
113  return $widget;
114  }
115 
116  protected function shouldInfuseOOUI() {
117  return true;
118  }
119 
120  protected function getOOUIModules() {
121  return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
122  }
123 
124 }
HTMLUsersMultiselectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUsersMultiselectField.php:30
MediaWiki\Widget\UsersMultiselectWidget
Widget to select multiple users.
Definition: UsersMultiselectWidget.php:11
HTMLUsersMultiselectField\loadDataFromRequest
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
Definition: HTMLUsersMultiselectField.php:18
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:13
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:19
HTMLUsersMultiselectField
Implements a tag multiselect input field for user names.
Definition: HTMLUsersMultiselectField.php:17
HTMLUsersMultiselectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLUsersMultiselectField.php:56
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:960
HTMLUsersMultiselectField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUsersMultiselectField.php:120
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:83
HTMLUsersMultiselectField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUsersMultiselectField.php:116
HTMLUsersMultiselectField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLUsersMultiselectField.php:61