Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLFileField.php
blob14729a5a9ecd7903e1efbe4de3056a41c2ca79f0
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7 use OOUI\Widget;
9 /**
10 * File <input> field.
12 * Besides the parameters recognized by HTMLFormField, the following are
13 * recognized:
14 * placeholder/placeholder-message - HTML placeholder attribute
15 * accept - Array of acceptable MIME types/extensions to show in file chooser,
16 * null to accept all files.
17 * multiple - Allow multiple files to be selected
19 * @stable to extend
21 class HTMLFileField extends HTMLFormField {
22 /** @var string */
23 protected $mPlaceholder = '';
24 /** @var string[]|null */
25 protected $mAccept = null;
27 /** @var bool */
28 protected $mMultiple;
30 /**
31 * @stable to call
33 * @param array $params
34 * - placeholder/placeholder-message
35 * - accept
36 * - multiple
38 public function __construct( $params ) {
39 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
40 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
43 parent::__construct( $params );
45 if ( isset( $params['placeholder-message'] ) ) {
46 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
47 } elseif ( isset( $params['placeholder'] ) ) {
48 $this->mPlaceholder = $params['placeholder'];
51 $this->mAccept = $params['accept'] ?? null;
52 $this->mMultiple = !empty( $params['multiple'] );
55 /**
56 * @inheritDoc
58 public function loadDataFromRequest( $request ) {
59 return $request->getUpload( $this->mName )->getName();
62 /**
63 * @inheritDoc
64 * @stable to override
66 public function getInputHTML( $value ) {
67 $attribs = [
68 'id' => $this->mID,
69 'name' => $this->mName,
70 'dir' => $this->mDir,
71 ] + $this->getTooltipAndAccessKey();
73 if ( $this->mClass !== '' ) {
74 $attribs['class'] = $this->mClass;
76 if ( $this->mAccept ) {
77 $attribs['accept'] = implode( ',', $this->mAccept );
79 if ( $this->mMultiple ) {
80 $attribs['multiple'] = '';
82 // Note: Placeholders are not supported by native file inputs
84 $allowedParams = [
85 'title',
86 'tabindex',
87 'disabled',
88 'required',
89 'autofocus',
90 'readonly',
93 $attribs += $this->getAttributes( $allowedParams );
95 return Html::input( $this->mName, $value ?? '', 'file', $attribs );
98 /**
99 * @inheritDoc
100 * @stable to override
102 public function getInputOOUI( $value ) {
103 $attribs = $this->getTooltipAndAccessKeyOOUI();
105 if ( $this->mClass !== '' ) {
106 $attribs['classes'] = [ $this->mClass ];
108 if ( $this->mPlaceholder !== '' ) {
109 $attribs['placeholder'] = $this->mPlaceholder;
111 if ( $this->mAccept ) {
112 $attribs['accept'] = $this->mAccept;
114 if ( $this->mMultiple ) {
115 $attribs['multiple'] = true;
118 # @todo Enforce pattern, step, required, readonly on the server side as
119 # well
120 $allowedParams = [
121 'title',
122 'tabindex',
123 'disabled',
124 'required',
125 'autofocus',
126 'readonly',
129 $attribs += \OOUI\Element::configFromHtmlAttributes(
130 $this->getAttributes( $allowedParams )
133 return $this->getInputWidget( [
134 'id' => $this->mID,
135 'name' => $this->mName,
136 'dir' => $this->mDir,
137 ] + $attribs );
141 * @stable to override
143 * @param array $params
145 * @return Widget
147 protected function getInputWidget( $params ) {
148 return new \OOUI\SelectFileInputWidget( $params );
152 * @inheritDoc
153 * @stable to override
155 protected function shouldInfuseOOUI() {
156 return true;
160 /** @deprecated class alias since 1.42 */
161 class_alias( HTMLFileField::class, 'HTMLFileField' );