3 use MediaWiki\Widget\UsersMultiselectWidget
;
6 * Implements a capsule multiselect input field for user names.
8 * Besides the parameters recognized by HTMLUserTextField, additional recognized
10 * default - (optional) Array of usernames to use as preset data
11 * placeholder - (optional) Custom placeholder message for input
13 * The result is the array of usernames
15 * @note This widget is not likely to remain functional in non-OOUI forms.
17 class HTMLUsersMultiselectField
extends HTMLUserTextField
{
18 public function loadDataFromRequest( $request ) {
19 $value = $request->getText( $this->mName
, $this->getDefault() );
21 $usersArray = explode( "\n", $value );
23 $usersArray = array_values( array_filter( $usersArray, function ( $username ) {
24 return trim( $username ) !== '';
26 // This function is expected to return a string
27 return implode( "\n", $usersArray );
30 public function validate( $value, $alldata ) {
31 if ( !$this->mParams
['exists'] ) {
35 if ( is_null( $value ) ) {
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 ) {
51 public function getInputHTML( $value ) {
52 $this->mParent
->getOutput()->enableOOUI();
53 return $this->getInputOOUI( $value );
56 public function getInputOOUI( $value ) {
57 $params = [ 'name' => $this->mName
];
59 if ( isset( $this->mParams
['default'] ) ) {
60 $params['default'] = $this->mParams
['default'];
63 if ( isset( $this->mParams
['placeholder'] ) ) {
64 $params['placeholder'] = $this->mParams
['placeholder'];
66 $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )
71 if ( !is_null( $value ) ) {
72 // $value is a string, but the widget expects an array
73 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
76 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
77 $params['infusable'] = true;
78 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
79 $widget = new UsersMultiselectWidget( $params );
80 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
85 protected function shouldInfuseOOUI() {
89 protected function getOOUIModules() {
90 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];