HTMLUsersMultiselectField.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use MediaWiki\Widget\UsersMultiselectWidget;
  3. /**
  4. * Implements a tag multiselect input field for user names.
  5. *
  6. * Besides the parameters recognized by HTMLUserTextField, additional recognized
  7. * parameters are:
  8. * default - (optional) Array of usernames to use as preset data
  9. * placeholder - (optional) Custom placeholder message for input
  10. *
  11. * The result is the array of usernames
  12. *
  13. * @note This widget is not likely to remain functional in non-OOUI forms.
  14. */
  15. class HTMLUsersMultiselectField extends HTMLUserTextField {
  16. public function loadDataFromRequest( $request ) {
  17. $value = $request->getText( $this->mName, $this->getDefault() );
  18. $usersArray = explode( "\n", $value );
  19. // Remove empty lines
  20. $usersArray = array_values( array_filter( $usersArray, function ( $username ) {
  21. return trim( $username ) !== '';
  22. } ) );
  23. // This function is expected to return a string
  24. return implode( "\n", $usersArray );
  25. }
  26. public function validate( $value, $alldata ) {
  27. if ( !$this->mParams['exists'] ) {
  28. return true;
  29. }
  30. if ( is_null( $value ) ) {
  31. return false;
  32. }
  33. // $value is a string, because HTMLForm fields store their values as strings
  34. $usersArray = explode( "\n", $value );
  35. foreach ( $usersArray as $username ) {
  36. $result = parent::validate( $username, $alldata );
  37. if ( $result !== true ) {
  38. return $result;
  39. }
  40. }
  41. return true;
  42. }
  43. public function getInputHTML( $value ) {
  44. $this->mParent->getOutput()->enableOOUI();
  45. return $this->getInputOOUI( $value );
  46. }
  47. public function getInputOOUI( $value ) {
  48. $params = [ 'name' => $this->mName ];
  49. if ( isset( $this->mParams['id'] ) ) {
  50. $params['id'] = $this->mParams['id'];
  51. }
  52. if ( isset( $this->mParams['disabled'] ) ) {
  53. $params['disabled'] = $this->mParams['disabled'];
  54. }
  55. if ( isset( $this->mParams['default'] ) ) {
  56. $params['default'] = $this->mParams['default'];
  57. }
  58. if ( isset( $this->mParams['placeholder'] ) ) {
  59. $params['placeholder'] = $this->mParams['placeholder'];
  60. } else {
  61. $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )->plain();
  62. }
  63. if ( !is_null( $value ) ) {
  64. // $value is a string, but the widget expects an array
  65. $params['default'] = $value === '' ? [] : explode( "\n", $value );
  66. }
  67. // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
  68. $params['infusable'] = true;
  69. $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
  70. $widget = new UsersMultiselectWidget( $params );
  71. $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
  72. return $widget;
  73. }
  74. protected function shouldInfuseOOUI() {
  75. return true;
  76. }
  77. protected function getOOUIModules() {
  78. return [ 'mediawiki.widgets.UsersMultiselectWidget' ];
  79. }
  80. }