MediaWiki  master
HTMLFileField.php
Go to the documentation of this file.
1 <?php
2 
4 use OOUI\Widget;
5 
19  protected $mPlaceholder = '';
20  protected $mAccept = null;
21 
23  protected $mMultiple;
24 
33  public function __construct( $params ) {
34  if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
35  $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
36  }
37 
38  parent::__construct( $params );
39 
40  if ( isset( $params['placeholder-message'] ) ) {
41  $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
42  } elseif ( isset( $params['placeholder'] ) ) {
43  $this->mPlaceholder = $params['placeholder'];
44  }
45 
46  $this->mAccept = $params['accept'] ?? null;
47  $this->mMultiple = !empty( $params['multiple'] );
48  }
49 
54  public function getInputHTML( $value ) {
55  $attribs = [
56  'id' => $this->mID,
57  'name' => $this->mName,
58  'dir' => $this->mDir,
59  ] + $this->getTooltipAndAccessKey();
60 
61  if ( $this->mClass !== '' ) {
62  $attribs['class'] = $this->mClass;
63  }
64  if ( $this->mAccept ) {
65  $attribs['accept'] = implode( ',', $this->mAccept );
66  }
67  if ( $this->mMultiple ) {
68  $attribs['multiple'] = '';
69  }
70  // Note: Placeholders are not supported by native file inputs
71 
72  $allowedParams = [
73  'title',
74  'tabindex',
75  'disabled',
76  'required',
77  'autofocus',
78  'readonly',
79  ];
80 
81  $attribs += $this->getAttributes( $allowedParams );
82 
83  return Html::input( $this->mName, $value, 'file', $attribs );
84  }
85 
90  public function getInputOOUI( $value ) {
91  $attribs = $this->getTooltipAndAccessKeyOOUI();
92 
93  if ( $this->mClass !== '' ) {
94  $attribs['classes'] = [ $this->mClass ];
95  }
96  if ( $this->mPlaceholder !== '' ) {
97  $attribs['placeholder'] = $this->mPlaceholder;
98  }
99  if ( $this->mAccept ) {
100  $attribs['accept'] = $this->mAccept;
101  }
102  if ( $this->mMultiple ) {
103  $attribs['multiple'] = true;
104  }
105 
106  # @todo Enforce pattern, step, required, readonly on the server side as
107  # well
108  $allowedParams = [
109  'title',
110  'tabindex',
111  'disabled',
112  'required',
113  'autofocus',
114  'readonly',
115  ];
116 
117  $attribs += OOUI\Element::configFromHtmlAttributes(
118  $this->getAttributes( $allowedParams )
119  );
120 
121  return $this->getInputWidget( [
122  'id' => $this->mID,
123  'name' => $this->mName,
124  'dir' => $this->mDir,
125  ] + $attribs );
126  }
127 
135  protected function getInputWidget( $params ) {
136  return new OOUI\SelectFileInputWidget( $params );
137  }
138 
143  protected function shouldInfuseOOUI() {
144  return true;
145  }
146 }
File <input> field.
getInputWidget( $params)
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
__construct( $params)
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
getAttributes(array $list)
Returns the given attributes from the parameters.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57