MediaWiki  master
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 }
HTMLUsersMultiselectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUsersMultiselectField.php:31
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:19
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:15
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:20
HTMLUsersMultiselectField
Implements a tag multiselect input field for user names.
Definition: HTMLUsersMultiselectField.php:18
HTMLUsersMultiselectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition: HTMLUsersMultiselectField.php:57
HTMLFormField\getDefault
getDefault()
Stable to override.
Definition: HTMLFormField.php:999
HTMLUsersMultiselectField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUsersMultiselectField.php:121
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLUsersMultiselectField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUsersMultiselectField.php:117
HTMLUsersMultiselectField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
Definition: HTMLUsersMultiselectField.php:62