MediaWiki  1.34.4
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 ( is_null( $value ) ) {
36  return false;
37  }
38 
39  // $value is a string, because HTMLForm fields store their values as strings
40  $usersArray = explode( "\n", $value );
41  foreach ( $usersArray as $username ) {
42  $result = parent::validate( $username, $alldata );
43  if ( $result !== true ) {
44  return $result;
45  }
46  }
47 
48  return true;
49  }
50 
51  public function getInputHTML( $value ) {
52  $this->mParent->getOutput()->enableOOUI();
53  return $this->getInputOOUI( $value );
54  }
55 
56  public function getInputOOUI( $value ) {
57  $params = [ 'name' => $this->mName ];
58 
59  if ( isset( $this->mParams['id'] ) ) {
60  $params['id'] = $this->mParams['id'];
61  }
62 
63  if ( isset( $this->mParams['disabled'] ) ) {
64  $params['disabled'] = $this->mParams['disabled'];
65  }
66 
67  if ( isset( $this->mParams['default'] ) ) {
68  $params['default'] = $this->mParams['default'];
69  }
70 
71  if ( isset( $this->mParams['placeholder'] ) ) {
72  $params['placeholder'] = $this->mParams['placeholder'];
73  } else {
74  $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
75  }
76 
77  if ( !is_null( $value ) ) {
78  // $value is a string, but the widget expects an array
79  $params['default'] = $value === '' ? [] : explode( "\n", $value );
80  }
81 
82  // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
83  $params['infusable'] = true;
84  $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
85  $widget = new UsersMultiselectWidget( $params );
86  $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
87 
88  return $widget;
89  }
90 
91  protected function shouldInfuseOOUI() {
92  return true;
93  }
94 
95  protected function getOOUIModules() {
96  return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
97  }
98 
99 }
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:18
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:51
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:959
HTMLUsersMultiselectField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUsersMultiselectField.php:95
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:91
HTMLUsersMultiselectField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLUsersMultiselectField.php:56