Merge "auth: Follow up on e907d4328dc3e"
[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
106 if ( isset( $this->mParams['input'] ) ) {
107 $params['input'] = $this->mParams['input'];
108 }
109
110 if ( !is_null( $value ) ) {
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-field-autoinfuse' ];
118 $widget = new TitlesMultiselectWidget( $params );
119 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
120
121 return $widget;
122 }
123
124 protected function shouldInfuseOOUI() {
125 return true;
126 }
127
128 protected function getOOUIModules() {
129 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
130 }
131
132 }