Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLTitlesMultiselectField.php
bloba9dadb86002f9d4de7568ebdcd9efe8a3845fbb4
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\Widget\TitlesMultiselectWidget;
7 /**
8 * Implements a tag multiselect input field for titles.
10 * Besides the parameters recognized by HTMLTitleTextField, additional recognized
11 * parameters are:
12 * default - (optional) Array of titles to use as preset data
13 * placeholder - (optional) Custom placeholder message for input
15 * The result is the array of titles
17 * This widget is a duplication of HTMLUsersMultiselectField, except for:
18 * - The configuration variable changed to 'titles' (from 'users')
19 * - OOUI modules were adjusted for the TitlesMultiselectWidget
20 * - The PHP version instantiates a MediaWiki\Widget\TitlesMultiselectWidget
22 * @stable to extend
23 * @note This widget is not likely to remain functional in non-OOUI forms.
25 class HTMLTitlesMultiselectField extends HTMLTitleTextField {
26 /**
27 * @stable to call
28 * @inheritDoc
30 public function __construct( $params ) {
31 $params += [
32 // This overrides the default from HTMLTitleTextField
33 'required' => false,
36 parent::__construct( $params );
39 public function loadDataFromRequest( $request ) {
40 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
42 $titlesArray = explode( "\n", $value );
43 // Remove empty lines
44 $titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) {
45 return trim( $title ) !== '';
46 } ) );
47 // This function is expected to return a string
48 return implode( "\n", $titlesArray );
51 public function validate( $value, $alldata ) {
52 if ( !$this->mParams['exists'] ) {
53 return true;
56 if ( $value === null ) {
57 return false;
60 // $value is a string, because HTMLForm fields store their values as strings
61 $titlesArray = explode( "\n", $value );
63 if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
64 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
67 foreach ( $titlesArray as $title ) {
68 $result = parent::validate( $title, $alldata );
69 if ( $result !== true ) {
70 return $result;
74 return true;
77 public function getInputHTML( $value ) {
78 $this->mParent->getOutput()->enableOOUI();
79 return $this->getInputOOUI( $value );
82 public function getInputOOUI( $value ) {
83 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
85 $params = [
86 'id' => $this->mID,
87 'name' => $this->mName,
88 'dir' => $this->mDir,
91 if ( isset( $this->mParams['disabled'] ) ) {
92 $params['disabled'] = $this->mParams['disabled'];
95 if ( isset( $this->mParams['default'] ) ) {
96 $params['default'] = $this->mParams['default'];
99 $params['placeholder'] = $this->mParams['placeholder'] ??
100 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
102 if ( isset( $this->mParams['max'] ) ) {
103 $params['tagLimit'] = $this->mParams['max'];
106 if ( isset( $this->mParams['showMissing'] ) ) {
107 $params['showMissing'] = $this->mParams['showMissing'];
109 if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
110 $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
112 if ( isset( $this->mParams['allowEditTags'] ) ) {
113 $params['allowEditTags'] = $this->mParams['allowEditTags'];
116 if ( isset( $this->mParams['input'] ) ) {
117 $params['input'] = $this->mParams['input'];
120 if ( $value !== null ) {
121 // $value is a string, but the widget expects an array
122 $params['default'] = $value === '' ? [] : explode( "\n", $value );
125 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
126 $params['infusable'] = true;
127 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
129 return $this->getInputWidget( $params );
133 * @inheritDoc
135 protected function getInputWidget( $params ) {
136 $widget = new TitlesMultiselectWidget( $params );
137 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
138 return $widget;
141 protected function shouldInfuseOOUI() {
142 return true;
145 protected function getOOUIModules() {
146 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
149 public function getInputCodex( $value, $hasErrors ) {
150 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
151 // Use 10 instead (but allow $this->mParams to override that value)
152 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
153 return $textAreaField->getInputCodex( $value, $hasErrors );
158 /** @deprecated class alias since 1.42 */
159 class_alias( HTMLTitlesMultiselectField::class, 'HTMLTitlesMultiselectField' );