MediaWiki master
HTMLTagMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
6
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
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
138class_alias( HTMLTagMultiselectField::class, 'HTMLTagMultiselectField' );
array $params
The job parameters.
Implements a tag multiselect input field for arbitrary values.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
shouldInfuseOOUI()
Whether the field should be automatically infused.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
msg( $key,... $params)
Get a translated interface message.
Base class for widgets to select multiple users, titles, namespaces, etc.