MediaWiki  master
HTMLTitlesMultiselectField.php
Go to the documentation of this file.
1 <?php
2 
4 
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'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) {
57  return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
58  }
59 
60  foreach ( $titlesArray as $title ) {
61  $result = parent::validate( $title, $alldata );
62  if ( $result !== true ) {
63  return $result;
64  }
65  }
66 
67  return true;
68  }
69 
70  public function getInputHTML( $value ) {
71  $this->mParent->getOutput()->enableOOUI();
72  return $this->getInputOOUI( $value );
73  }
74 
75  public function getInputOOUI( $value ) {
76  $params = [
77  'id' => $this->mID,
78  'name' => $this->mName,
79  'dir' => $this->mDir,
80  ];
81 
82  if ( isset( $this->mParams['disabled'] ) ) {
83  $params['disabled'] = $this->mParams['disabled'];
84  }
85 
86  if ( isset( $this->mParams['default'] ) ) {
87  $params['default'] = $this->mParams['default'];
88  }
89 
90  if ( isset( $this->mParams['placeholder'] ) ) {
91  $params['placeholder'] = $this->mParams['placeholder'];
92  } else {
93  $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
94  }
95 
96  if ( isset( $this->mParams['max'] ) ) {
97  $params['tagLimit'] = $this->mParams['max'];
98  }
99 
100  if ( isset( $this->mParams['showMissing'] ) ) {
101  $params['showMissing'] = $this->mParams['showMissing'];
102  }
103  if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) {
104  $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces'];
105  }
106 
107  if ( isset( $this->mParams['input'] ) ) {
108  $params['input'] = $this->mParams['input'];
109  }
110 
111  if ( !is_null( $value ) ) {
112  // $value is a string, but the widget expects an array
113  $params['default'] = $value === '' ? [] : explode( "\n", $value );
114  }
115 
116  // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
117  $params['infusable'] = true;
118  $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
119  $widget = new TitlesMultiselectWidget( $params );
120  $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
121 
122  return $widget;
123  }
124 
125  protected function shouldInfuseOOUI() {
126  return true;
127  }
128 
129  protected function getOOUIModules() {
130  return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];
131  }
132 
133 }
Implements a tag multiselect input field for titles.
Implements a text input field for page titles.
msg( $key,... $params)
Get a translated interface message.