3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\Html\Html
;
6 use MediaWiki\HTMLForm\HTMLFormField
;
12 * Besides the parameters recognized by HTMLFormField, the following are
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
21 class HTMLFileField
extends HTMLFormField
{
23 protected $mPlaceholder = '';
24 /** @var string[]|null */
25 protected $mAccept = null;
33 * @param array $params
34 * - placeholder/placeholder-message
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'] );
58 public function loadDataFromRequest( $request ) {
59 return $request->getUpload( $this->mName
)->getName();
66 public function getInputHTML( $value ) {
69 'name' => $this->mName
,
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
93 $attribs +
= $this->getAttributes( $allowedParams );
95 return Html
::input( $this->mName
, $value ??
'', 'file', $attribs );
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
129 $attribs +
= \OOUI\Element
::configFromHtmlAttributes(
130 $this->getAttributes( $allowedParams )
133 return $this->getInputWidget( [
135 'name' => $this->mName
,
136 'dir' => $this->mDir
,
141 * @stable to override
143 * @param array $params
147 protected function getInputWidget( $params ) {
148 return new \OOUI\
SelectFileInputWidget( $params );
153 * @stable to override
155 protected function shouldInfuseOOUI() {
160 /** @deprecated class alias since 1.42 */
161 class_alias( HTMLFileField
::class, 'HTMLFileField' );