Merge "debug: Add basic accessibility support to debug console"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLTitlesMultiselectField.php
1 <?php
2
3 use MediaWiki\Widget\TitlesMultiselectWidget;
4
5 /**
6 * Implements a tag multiselect input field for titles.
7 *
8 * Besides the parameters recognized by HTMLTitleTextField, additional recognized
9 * parameters are:
10 * default - (optional) Array of titles to use as preset data
11 * placeholder - (optional) Custom placeholder message for input
12 *
13 * The result is the array of titles
14 *
15 * This widget is a duplication of HTMLUsersMultiselectField, except for:
16 * - The configuration variable changed to 'titles' (from 'users')
17 * - OOUI modules were adjusted for the TitlesMultiselectWidget
18 * - The PHP version instantiates a MediaWiki\Widget\TitlesMultiselectWidget
19 *
20 * @note This widget is not likely to remain functional in non-OOUI forms.
21 */
22 class HTMLTitlesMultiselectField extends HTMLTitleTextField {
23 public function __construct( $params ) {
24 $params += [
25 // This overrides the default from HTMLTitleTextField
26 'required' => false,
27 ];
28
29 parent::__construct( $params );
30 }
31
32 public function loadDataFromRequest( $request ) {
33 $value = $request->getText( $this->mName, $this->getDefault() );
34
35 $titlesArray = explode( "\n", $value );
36 // Remove empty lines
37 $titlesArray = array_values( array_filter( $titlesArray, function ( $title ) {
38 return trim( $title ) !== '';
39 } ) );
40 // This function is expected to return a string
41 return implode( "\n", $titlesArray );
42 }
43
44 public function validate( $value, $alldata ) {
45 if ( !$this->mParams['exists'] ) {
46 return true;
47 }
48
49 if ( is_null( $value ) ) {
50 return false;
51 }
52
53 // $value is a string, because HTMLForm fields store their values as strings
54 $titlesArray = explode( "\n", $value );
55
56 if ( isset( $this->mParams['max'] ) ) {
57 if ( count( $titlesArray ) > $this->mParams['max'] ) {
58 return $this->msg( 'htmlform-int-toohigh', $this->mParams['max'] );
59 }
60 }
61
62 foreach ( $titlesArray as $title ) {
63 $result = parent::validate( $title, $alldata );
64 if ( $result !== true ) {
65 return $result;
66 }
67 }
68
69 return true;
70 }
71
72 public function getInputHTML( $value ) {
73 $this->mParent->getOutput()->enableOOUI();
74 return $this->getInputOOUI( $value );
75 }
76
77 public function getInputOOUI( $value ) {
78 $params = [
79 'id' => $this->mID,
80 'name' => $this->mName,
81 'dir' => $this->mDir,
82 ];
83
84 if ( isset( $this->mParams['disabled'] ) ) {
85 $params['disabled'] = $this->mParams['disabled'];
86 }
87
88 if ( isset( $this->mParams['default'] ) ) {
89 $params['default'] = $this->mParams['default'];
90 }
91
92 if ( isset( $this->mParams['placeholder'] ) ) {
93 $params['placeholder'] = $this->mParams['placeholder'];
94 } else {
95 $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
96 }
97
98 if ( isset( $this->mParams['max'] ) ) {
99 $params['tagLimit'] = $this->mParams['max'];
100 }
101
102 if ( isset( $this->mParams['showMissing'] ) ) {
103 $params['showMissing'] = $this->mParams['showMissing'];
104 }
105 if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
106 $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
107 }
108
109 if ( isset( $this->mParams['input'] ) ) {
110 $params['input'] = $this->mParams['input'];
111 }
112
113 if ( !is_null( $value ) ) {
114 // $value is a string, but the widget expects an array
115 $params['default'] = $value === '' ? [] : explode( "\n", $value );
116 }
117
118 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
119 $params['infusable'] = true;
120 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
121 $widget = new TitlesMultiselectWidget( $params );
122 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
123
124 return $widget;
125 }
126
127 protected function shouldInfuseOOUI() {
128 return true;
129 }
130
131 protected function getOOUIModules() {
132 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
133 }
134
135 }