3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\Widget\TagMultiselectWidget
;
8 * Implements a tag multiselect input field for arbitrary values.
10 * Besides the parameters recognized by HTMLTextField, additional recognized
12 * allowArbitrary - (optional) Bool to allow arbitrary inputs
13 * allowedValues - (optional) Array of allowed values
15 * The result is the array of tags
18 * @note This widget is not likely to remain functional in non-OOUI forms.
20 class HTMLTagMultiselectField
extends HTMLTextField
{
21 public function loadDataFromRequest( $request ) {
22 $value = $request->getText( $this->mName
, $this->getDefault() ??
'' );
24 $tagsArray = explode( "\n", $value );
26 $tagsArray = array_values( array_filter( $tagsArray, static function ( $tag ) {
27 return trim( $tag ) !== '';
29 // Remove any duplicate tags
30 $uniqueTags = array_unique( $tagsArray );
32 // This function is expected to return a string
33 return implode( "\n", $uniqueTags );
36 public function validate( $value, $alldata ) {
37 if ( $value === null ) {
41 // $value is a string, because HTMLForm fields store their values as strings
42 $tagsArray = explode( "\n", $value );
44 if ( isset( $this->mParams
['max'] ) && ( count( $tagsArray ) > $this->mParams
['max'] ) ) {
45 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams
['max'] );
48 foreach ( $tagsArray as $tag ) {
49 $result = parent
::validate( $tag, $alldata );
50 if ( $result !== true ) {
54 if ( empty( $this->mParams
['allowArbitrary'] ) && $tag ) {
55 $allowedValues = $this->mParams
['allowedValues'] ??
[];
56 if ( !in_array( $tag, $allowedValues ) ) {
57 return $this->msg( 'htmlform-tag-not-allowed', $tag )->escaped();
65 public function getInputHTML( $value ) {
66 $this->mParent
->getOutput()->enableOOUI();
67 return $this->getInputOOUI( $value );
70 public function getInputOOUI( $value ) {
71 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
73 $params = [ 'name' => $this->mName
];
75 if ( isset( $this->mParams
['id'] ) ) {
76 $params['id'] = $this->mParams
['id'];
79 if ( isset( $this->mParams
['disabled'] ) ) {
80 $params['disabled'] = $this->mParams
['disabled'];
83 if ( isset( $this->mParams
['default'] ) ) {
84 $params['default'] = $this->mParams
['default'];
87 $params['placeholder'] = $this->mParams
['placeholder'] ??
88 $this->msg( 'mw-widgets-tagmultiselect-placeholder' )->plain();
90 if ( isset( $this->mParams
['max'] ) ) {
91 $params['tagLimit'] = $this->mParams
['max'];
94 if ( isset( $this->mParams
['allowArbitrary'] ) ) {
95 $params['allowArbitrary'] = $this->mParams
['allowArbitrary'];
98 if ( isset( $this->mParams
['allowedValues'] ) ) {
99 $params['allowedValues'] = $this->mParams
['allowedValues'];
102 if ( isset( $this->mParams
['input'] ) ) {
103 $params['input'] = $this->mParams
['input'];
106 if ( $value !== null ) {
107 // $value is a string, but the widget expects an array
108 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
111 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
112 $params['infusable'] = true;
113 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
115 return $this->getInputWidget( $params );
121 protected function getInputWidget( $params ) {
122 $widget = new TagMultiselectWidget( $params );
123 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
127 protected function shouldInfuseOOUI() {
131 protected function getOOUIModules() {
132 return [ 'mediawiki.widgets.TagMultiselectWidget' ];
137 /** @deprecated class alias since 1.42 */
138 class_alias( HTMLTagMultiselectField
::class, 'HTMLTagMultiselectField' );