HTMLTitlesMultiselectField.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. use MediaWiki\Widget\TitlesMultiselectWidget;
  3. /**
  4. * Implements a tag multiselect input field for titles.
  5. *
  6. * Besides the parameters recognized by HTMLTitleTextField, additional recognized
  7. * parameters are:
  8. * default - (optional) Array of titles to use as preset data
  9. * placeholder - (optional) Custom placeholder message for input
  10. *
  11. * The result is the array of titles
  12. *
  13. * This widget is a duplication of HTMLUsersMultiselectField, except for:
  14. * - The configuration variable changed to 'titles' (from 'users')
  15. * - OOUI modules were adjusted for the TitlesMultiselectWidget
  16. * - The PHP version instantiates a MediaWiki\Widget\TitlesMultiselectWidget
  17. *
  18. * @note This widget is not likely to remain functional in non-OOUI forms.
  19. */
  20. class HTMLTitlesMultiselectField extends HTMLTitleTextField {
  21. public function __construct( $params ) {
  22. $params += [
  23. // This overrides the default from HTMLTitleTextField
  24. 'required' => false,
  25. ];
  26. parent::__construct( $params );
  27. }
  28. public function loadDataFromRequest( $request ) {
  29. $value = $request->getText( $this->mName, $this->getDefault() );
  30. $titlesArray = explode( "\n", $value );
  31. // Remove empty lines
  32. $titlesArray = array_values( array_filter( $titlesArray, function ( $title ) {
  33. return trim( $title ) !== '';
  34. } ) );
  35. // This function is expected to return a string
  36. return implode( "\n", $titlesArray );
  37. }
  38. public function validate( $value, $alldata ) {
  39. if ( !$this->mParams['exists'] ) {
  40. return true;
  41. }
  42. if ( is_null( $value ) ) {
  43. return false;
  44. }
  45. // $value is a string, because HTMLForm fields store their values as strings
  46. $titlesArray = explode( "\n", $value );
  47. if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
  48. return $this->msg( 'htmlform-int-toohigh', $this->mParams['max'] );
  49. }
  50. foreach ( $titlesArray as $title ) {
  51. $result = parent::validate( $title, $alldata );
  52. if ( $result !== true ) {
  53. return $result;
  54. }
  55. }
  56. return true;
  57. }
  58. public function getInputHTML( $value ) {
  59. $this->mParent->getOutput()->enableOOUI();
  60. return $this->getInputOOUI( $value );
  61. }
  62. public function getInputOOUI( $value ) {
  63. $params = [
  64. 'id' => $this->mID,
  65. 'name' => $this->mName,
  66. 'dir' => $this->mDir,
  67. ];
  68. if ( isset( $this->mParams['disabled'] ) ) {
  69. $params['disabled'] = $this->mParams['disabled'];
  70. }
  71. if ( isset( $this->mParams['default'] ) ) {
  72. $params['default'] = $this->mParams['default'];
  73. }
  74. if ( isset( $this->mParams['placeholder'] ) ) {
  75. $params['placeholder'] = $this->mParams['placeholder'];
  76. } else {
  77. $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
  78. }
  79. if ( isset( $this->mParams['max'] ) ) {
  80. $params['tagLimit'] = $this->mParams['max'];
  81. }
  82. if ( isset( $this->mParams['showMissing'] ) ) {
  83. $params['showMissing'] = $this->mParams['showMissing'];
  84. }
  85. if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
  86. $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
  87. }
  88. if ( isset( $this->mParams['input'] ) ) {
  89. $params['input'] = $this->mParams['input'];
  90. }
  91. if ( !is_null( $value ) ) {
  92. // $value is a string, but the widget expects an array
  93. $params['default'] = $value === '' ? [] : explode( "\n", $value );
  94. }
  95. // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
  96. $params['infusable'] = true;
  97. $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
  98. $widget = new TitlesMultiselectWidget( $params );
  99. $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
  100. return $widget;
  101. }
  102. protected function shouldInfuseOOUI() {
  103. return true;
  104. }
  105. protected function getOOUIModules() {
  106. return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
  107. }
  108. }