TagMultiselectWidget.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace MediaWiki\Widget;
  3. use OOUI\MultilineTextInputWidget;
  4. /**
  5. * Abstract base class for widgets to select multiple users, titles,
  6. * namespaces, etc.
  7. *
  8. * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
  9. * @license MIT
  10. */
  11. abstract class TagMultiselectWidget extends \OOUI\Widget {
  12. /** @var array */
  13. protected $selectedArray;
  14. /** @var string|null */
  15. protected $inputName;
  16. /** @var string|null */
  17. protected $inputPlaceholder;
  18. /** @var array */
  19. protected $input;
  20. /** @var int|null */
  21. protected $tagLimit;
  22. /**
  23. * @param array $config Configuration options
  24. * - array $config['default'] Array of items to use as preset data
  25. * - string $config['name'] Name attribute (used in forms)
  26. * - string $config['placeholder'] Placeholder message for input
  27. * - array $config['input'] Config options for the input widget
  28. * - int $config['tagLimit'] Maximum number of selected items
  29. */
  30. public function __construct( array $config = [] ) {
  31. parent::__construct( $config );
  32. // Properties
  33. $this->selectedArray = $config['default'] ?? [];
  34. $this->inputName = $config['name'] ?? null;
  35. $this->inputPlaceholder = $config['placeholder'] ?? null;
  36. $this->input = $config['input'] ?? [];
  37. $this->tagLimit = $config['tagLimit'] ?? null;
  38. $textarea = new MultilineTextInputWidget( array_merge( [
  39. 'name' => $this->inputName,
  40. 'value' => implode( "\n", $this->selectedArray ),
  41. 'rows' => 10,
  42. 'classes' => [
  43. 'mw-widgets-tagMultiselectWidget-multilineTextInputWidget'
  44. ],
  45. ], $this->input ) );
  46. $pending = new PendingTextInputWidget();
  47. $this->appendContent( $textarea, $pending );
  48. $this->addClasses( [ 'mw-widgets-tagMultiselectWidget' ] );
  49. }
  50. public function getConfig( &$config ) {
  51. if ( $this->selectedArray !== null ) {
  52. $config['selected'] = $this->selectedArray;
  53. }
  54. if ( $this->inputName !== null ) {
  55. $config['name'] = $this->inputName;
  56. }
  57. if ( $this->inputPlaceholder !== null ) {
  58. $config['placeholder'] = $this->inputPlaceholder;
  59. }
  60. if ( $this->input !== null ) {
  61. $config['input'] = $this->input;
  62. }
  63. if ( $this->tagLimit !== null ) {
  64. $config['tagLimit'] = $this->tagLimit;
  65. }
  66. $config['$overlay'] = true;
  67. return parent::getConfig( $config );
  68. }
  69. }