MediaWiki master
HTMLTagMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
6
22 public function loadDataFromRequest( $request ) {
23 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
24
25 $tagsArray = explode( "\n", $value );
26 // Remove empty lines
27 $tagsArray = array_values( array_filter( $tagsArray, static function ( $tag ) {
28 return trim( $tag ) !== '';
29 } ) );
30 // Remove any duplicate tags
31 $uniqueTags = array_unique( $tagsArray );
32
33 // This function is expected to return a string
34 return implode( "\n", $uniqueTags );
35 }
36
38 public function validate( $value, $alldata ) {
39 if ( $value === null ) {
40 return false;
41 }
42
43 // $value is a string, because HTMLForm fields store their values as strings
44 $tagsArray = explode( "\n", $value );
45
46 if ( isset( $this->mParams['max'] ) && ( count( $tagsArray ) > $this->mParams['max'] ) ) {
47 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
48 }
49
50 foreach ( $tagsArray as $tag ) {
51 $result = parent::validate( $tag, $alldata );
52 if ( $result !== true ) {
53 return $result;
54 }
55
56 if ( empty( $this->mParams['allowArbitrary'] ) && $tag ) {
57 $allowedValues = $this->mParams['allowedValues'] ?? [];
58 if ( !in_array( $tag, $allowedValues ) ) {
59 return $this->msg( 'htmlform-tag-not-allowed', $tag )->escaped();
60 }
61 }
62 }
63
64 return true;
65 }
66
68 public function getInputHTML( $value ) {
69 $this->mParent->getOutput()->enableOOUI();
70 return $this->getInputOOUI( $value )->toString();
71 }
72
74 public function getInputOOUI( $value ) {
75 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
76
77 $params = [ 'name' => $this->mName ];
78
79 if ( isset( $this->mParams['id'] ) ) {
80 $params['id'] = $this->mParams['id'];
81 }
82
83 if ( isset( $this->mParams['disabled'] ) ) {
84 $params['disabled'] = $this->mParams['disabled'];
85 }
86
87 if ( isset( $this->mParams['default'] ) ) {
88 $params['default'] = $this->mParams['default'];
89 }
90
91 $params['placeholder'] = $this->mParams['placeholder'] ??
92 $this->msg( 'mw-widgets-tagmultiselect-placeholder' )->plain();
93
94 if ( isset( $this->mParams['max'] ) ) {
95 $params['tagLimit'] = $this->mParams['max'];
96 }
97
98 if ( isset( $this->mParams['allowArbitrary'] ) ) {
99 $params['allowArbitrary'] = $this->mParams['allowArbitrary'];
100 }
101
102 if ( isset( $this->mParams['allowedValues'] ) ) {
103 $params['allowedValues'] = $this->mParams['allowedValues'];
104 }
105
106 if ( isset( $this->mParams['input'] ) ) {
107 $params['input'] = $this->mParams['input'];
108 }
109
110 if ( $value !== null ) {
111 // $value is a string, but the widget expects an array
112 $params['default'] = $value === '' ? [] : explode( "\n", $value );
113 }
114
115 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
116 $params['infusable'] = true;
117 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
118
119 return $this->getInputWidget( $params );
120 }
121
125 protected function getInputWidget( $params ) {
126 $widget = new TagMultiselectWidget( $params );
127 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
128 return $widget;
129 }
130
132 protected function shouldInfuseOOUI() {
133 return true;
134 }
135
137 protected function getOOUIModules() {
138 return [ 'mediawiki.widgets.TagMultiselectWidget' ];
139 }
140
141}
142
144class_alias( HTMLTagMultiselectField::class, 'HTMLTagMultiselectField' );
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.Don't forget to call pare...
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.Note that all OOUI HTMLForm fields are infusable (y...
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.