Debian Codegraph @

includes/htmlform/fields/HTMLTagMultiselectField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\HTMLForm\Field;
4
5 use MediaWiki\Widget\TagMultiselectWidget;
6
7 /**
8 * Implements a tag multiselect input field for arbitrary values.
9 *
10 * Besides the parameters recognized by HTMLTextField, additional recognized
11 * parameters are:
12 * allowArbitrary - (optional) Bool to allow arbitrary inputs
13 * allowedValues - (optional) Array of allowed values
14 *
15 * The result is the array of tags
16 *
17 * @stable to extend
18 * @note This widget is not likely to remain functional in non-OOUI forms.
19 */
20 class HTMLTagMultiselectField extends HTMLTextField {
21 public function loadDataFromRequest( $request ) {
22 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
23
24 $tagsArray = explode( "\n", $value );
25 // Remove empty lines
26 $tagsArray = array_values( array_filter( $tagsArray, static function ( $tag ) {
27 return trim( $tag ) !== '';
28 } ) );
29 // Remove any duplicate tags
30 $uniqueTags = array_unique( $tagsArray );
31
32 // This function is expected to return a string
33 return implode( "\n", $uniqueTags );
34 }
35
36 public function validate( $value, $alldata ) {
37 if ( $value === null ) {
38 return false;
39 }
40
41 // $value is a string, because HTMLForm fields store their values as strings
42 $tagsArray = explode( "\n", $value );
43
44 if ( isset( $this->mParams['max'] ) && ( count( $tagsArray ) > $this->mParams['max'] ) ) {
45 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
46 }
47
48 foreach ( $tagsArray as $tag ) {
49 $result = parent::validate( $tag, $alldata );
50 if ( $result !== true ) {
51 return $result;
52 }
53
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();
58 }
59 }
60 }
61
62 return true;
63 }
64
65 public function getInputHTML( $value ) {
66 $this->mParent->getOutput()->enableOOUI();
67 return $this->getInputOOUI( $value );
68 }
69
70 public function getInputOOUI( $value ) {
71 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
72
73 $params = [ 'name' => $this->mName ];
74
75 if ( isset( $this->mParams['id'] ) ) {
76 $params['id'] = $this->mParams['id'];
77 }
78
79 if ( isset( $this->mParams['disabled'] ) ) {
80 $params['disabled'] = $this->mParams['disabled'];
81 }
82
83 if ( isset( $this->mParams['default'] ) ) {
84 $params['default'] = $this->mParams['default'];
85 }
86
87 $params['placeholder'] = $this->mParams['placeholder'] ??
88 $this->msg( 'mw-widgets-tagmultiselect-placeholder' )->plain();
89
90 if ( isset( $this->mParams['max'] ) ) {
91 $params['tagLimit'] = $this->mParams['max'];
92 }
93
94 if ( isset( $this->mParams['allowArbitrary'] ) ) {
95 $params['allowArbitrary'] = $this->mParams['allowArbitrary'];
96 }
97
98 if ( isset( $this->mParams['allowedValues'] ) ) {
99 $params['allowedValues'] = $this->mParams['allowedValues'];
100 }
101
102 if ( isset( $this->mParams['input'] ) ) {
103 $params['input'] = $this->mParams['input'];
104 }
105
106 if ( $value !== null ) {
107 // $value is a string, but the widget expects an array
108 $params['default'] = $value === '' ? [] : explode( "\n", $value );
109 }
110
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' ];
114
115 return $this->getInputWidget( $params );
116 }
117
118 /**
119 * @inheritDoc
120 */
121 protected function getInputWidget( $params ) {
122 $widget = new TagMultiselectWidget( $params );
123 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
124 return $widget;
125 }
126
127 protected function shouldInfuseOOUI() {
128 return true;
129 }
130
131 protected function getOOUIModules() {
132 return [ 'mediawiki.widgets.TagMultiselectWidget' ];
133 }
134
135 }
136
137 /** @deprecated class alias since 1.42 */
138 class_alias( HTMLTagMultiselectField::class, 'HTMLTagMultiselectField' );