MediaWiki  master
HTMLTagMultiselectField.php
Go to the documentation of this file.
1 <?php
2 
4 
19  public function loadDataFromRequest( $request ) {
20  $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
21 
22  $tagsArray = explode( "\n", $value );
23  // Remove empty lines
24  $tagsArray = array_values( array_filter( $tagsArray, static function ( $tag ) {
25  return trim( $tag ) !== '';
26  } ) );
27  // Remove any duplicate tags
28  $uniqueTags = array_unique( $tagsArray );
29 
30  // This function is expected to return a string
31  return implode( "\n", $uniqueTags );
32  }
33 
34  public function validate( $value, $alldata ) {
35  if ( $value === null ) {
36  return false;
37  }
38 
39  // $value is a string, because HTMLForm fields store their values as strings
40  $tagsArray = explode( "\n", $value );
41 
42  if ( isset( $this->mParams['max'] ) && ( count( $tagsArray ) > $this->mParams['max'] ) ) {
43  return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
44  }
45 
46  foreach ( $tagsArray as $tag ) {
47  $result = parent::validate( $tag, $alldata );
48  if ( $result !== true ) {
49  return $result;
50  }
51 
52  if ( empty( $this->mParams['allowArbitrary'] ) && $tag ) {
53  $allowedValues = $this->mParams['allowedValues'] ?? [];
54  if ( !in_array( $tag, $allowedValues ) ) {
55  return $this->msg( 'htmlform-tag-not-allowed', $tag )->escaped();
56  }
57  }
58  }
59 
60  return true;
61  }
62 
63  public function getInputHTML( $value ) {
64  $this->mParent->getOutput()->enableOOUI();
65  return $this->getInputOOUI( $value );
66  }
67 
68  public function getInputOOUI( $value ) {
69  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
70 
71  $params = [ 'name' => $this->mName ];
72 
73  if ( isset( $this->mParams['id'] ) ) {
74  $params['id'] = $this->mParams['id'];
75  }
76 
77  if ( isset( $this->mParams['disabled'] ) ) {
78  $params['disabled'] = $this->mParams['disabled'];
79  }
80 
81  if ( isset( $this->mParams['default'] ) ) {
82  $params['default'] = $this->mParams['default'];
83  }
84 
85  $params['placeholder'] = $this->mParams['placeholder'] ??
86  $this->msg( 'mw-widgets-tagmultiselect-placeholder' )->plain();
87 
88  if ( isset( $this->mParams['max'] ) ) {
89  $params['tagLimit'] = $this->mParams['max'];
90  }
91 
92  if ( isset( $this->mParams['allowArbitrary'] ) ) {
93  $params['allowArbitrary'] = $this->mParams['allowArbitrary'];
94  }
95 
96  if ( isset( $this->mParams['allowedValues'] ) ) {
97  $params['allowedValues'] = $this->mParams['allowedValues'];
98  }
99 
100  if ( isset( $this->mParams['input'] ) ) {
101  $params['input'] = $this->mParams['input'];
102  }
103 
104  if ( $value !== null ) {
105  // $value is a string, but the widget expects an array
106  $params['default'] = $value === '' ? [] : explode( "\n", $value );
107  }
108 
109  // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
110  $params['infusable'] = true;
111  $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
112 
113  return $this->getInputWidget( $params );
114  }
115 
119  protected function getInputWidget( $params ) {
120  $widget = new TagMultiselectWidget( $params );
121  $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
122  return $widget;
123  }
124 
125  protected function shouldInfuseOOUI() {
126  return true;
127  }
128 
129  protected function getOOUIModules() {
130  return [ 'mediawiki.widgets.TagMultiselectWidget' ];
131  }
132 
133 }
msg( $key,... $params)
Get a translated interface message.
Implements a tag multiselect input field for arbitrary values.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputWidget( $params)
Stability: stableto overrideWidget
shouldInfuseOOUI()
Whether the field should be automatically infused.
<input> field.
Base class for widgets to select multiple users, titles, namespaces, etc.