MediaWiki
master
Toggle main menu visibility
HTMLTagMultiselectField.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\HTMLForm\Field;
4
5
use MediaWiki\Widget\TagMultiselectWidget;
6
20
class
HTMLTagMultiselectField
extends
HTMLTextField
{
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
$params[
'disabled'
] = $this->
isDisabledNoJs
( $this->mParent->mFieldData );
84
85
if
( isset( $this->mParams[
'default'
] ) ) {
86
$params[
'default'
] = $this->mParams[
'default'
];
87
}
88
89
$params[
'placeholder'
] = $this->mParams[
'placeholder'
] ??
90
$this->
msg
(
'mw-widgets-tagmultiselect-placeholder'
)->plain();
91
92
if
( isset( $this->mParams[
'max'
] ) ) {
93
$params[
'tagLimit'
] = $this->mParams[
'max'
];
94
}
95
96
if
( isset( $this->mParams[
'allowArbitrary'
] ) ) {
97
$params[
'allowArbitrary'
] = $this->mParams[
'allowArbitrary'
];
98
}
99
100
if
( isset( $this->mParams[
'allowedValues'
] ) ) {
101
$params[
'allowedValues'
] = $this->mParams[
'allowedValues'
];
102
}
103
104
if
( isset( $this->mParams[
'input'
] ) ) {
105
$params[
'input'
] = $this->mParams[
'input'
];
106
}
107
108
if
( $value !==
null
) {
109
// $value is a string, but the widget expects an array
110
$params[
'default'
] = $value ===
''
? [] : explode(
"\n"
, $value );
111
}
112
113
// Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
114
$params[
'infusable'
] =
true
;
115
$params[
'classes'
] = [
'mw-htmlform-autoinfuse'
];
116
117
return
$this->
getInputWidget
( $params );
118
}
119
123
protected
function
getInputWidget
( $params ) {
124
$widget =
new
TagMultiselectWidget
( $params );
125
$widget->setAttributes( [
'data-mw-modules'
=> implode(
','
, $this->
getOOUIModules
() ) ] );
126
return
$widget;
127
}
128
130
protected
function
shouldInfuseOOUI
() {
131
return
true
;
132
}
133
135
protected
function
getOOUIModules
() {
136
return
[
'mediawiki.widgets.TagMultiselectWidget'
];
137
}
138
139
}
140
142
class_alias( HTMLTagMultiselectField::class,
'HTMLTagMultiselectField'
);
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField
Implements a tag multiselect input field for arbitrary values.
Definition
HTMLTagMultiselectField.php:20
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
Definition
HTMLTagMultiselectField.php:74
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
Definition
HTMLTagMultiselectField.php:38
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition
HTMLTagMultiselectField.php:135
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
Definition
HTMLTagMultiselectField.php:130
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\loadDataFromRequest
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
Definition
HTMLTagMultiselectField.php:22
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\getInputWidget
getInputWidget( $params)
to overrideWidget
Definition
HTMLTagMultiselectField.php:123
MediaWiki\HTMLForm\Field\HTMLTagMultiselectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition
HTMLTagMultiselectField.php:68
MediaWiki\HTMLForm\Field\HTMLTextField
<input> field.
Definition
HTMLTextField.php:23
MediaWiki\HTMLForm\HTMLFormField\getDefault
getDefault()
Definition
HTMLFormField.php:1345
MediaWiki\HTMLForm\HTMLFormField\$mName
string $mName
Definition
HTMLFormField.php:37
MediaWiki\HTMLForm\HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition
HTMLFormField.php:138
MediaWiki\HTMLForm\HTMLFormField\isDisabledNoJs
isDisabledNoJs( $alldata)
Test whether this field is disabled in the generated HTML, either due to the 'disabled' parameter,...
Definition
HTMLFormField.php:437
MediaWiki\Widget\TagMultiselectWidget
Base class for widgets to select multiple users, titles, namespaces, etc.
Definition
TagMultiselectWidget.php:16
$request
$request
Definition
opensearch_desc.php:26
includes
HTMLForm
Field
HTMLTagMultiselectField.php
Generated on
for MediaWiki by
1.18.0