MediaWiki REL1_40
HTMLFileField.php
Go to the documentation of this file.
1<?php
2
4use 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
53 public function loadDataFromRequest( $request ) {
54 return $request->getUpload( $this->mName )->getName();
55 }
56
61 public function getInputHTML( $value ) {
62 $attribs = [
63 'id' => $this->mID,
64 'name' => $this->mName,
65 'dir' => $this->mDir,
66 ] + $this->getTooltipAndAccessKey();
67
68 if ( $this->mClass !== '' ) {
69 $attribs['class'] = $this->mClass;
70 }
71 if ( $this->mAccept ) {
72 $attribs['accept'] = implode( ',', $this->mAccept );
73 }
74 if ( $this->mMultiple ) {
75 $attribs['multiple'] = '';
76 }
77 // Note: Placeholders are not supported by native file inputs
78
79 $allowedParams = [
80 'title',
81 'tabindex',
82 'disabled',
83 'required',
84 'autofocus',
85 'readonly',
86 ];
87
88 $attribs += $this->getAttributes( $allowedParams );
89
90 return Html::input( $this->mName, $value ?? '', 'file', $attribs );
91 }
92
97 public function getInputOOUI( $value ) {
98 $attribs = $this->getTooltipAndAccessKeyOOUI();
99
100 if ( $this->mClass !== '' ) {
101 $attribs['classes'] = [ $this->mClass ];
102 }
103 if ( $this->mPlaceholder !== '' ) {
104 $attribs['placeholder'] = $this->mPlaceholder;
105 }
106 if ( $this->mAccept ) {
107 $attribs['accept'] = $this->mAccept;
108 }
109 if ( $this->mMultiple ) {
110 $attribs['multiple'] = true;
111 }
112
113 # @todo Enforce pattern, step, required, readonly on the server side as
114 # well
115 $allowedParams = [
116 'title',
117 'tabindex',
118 'disabled',
119 'required',
120 'autofocus',
121 'readonly',
122 ];
123
124 $attribs += OOUI\Element::configFromHtmlAttributes(
125 $this->getAttributes( $allowedParams )
126 );
127
128 return $this->getInputWidget( [
129 'id' => $this->mID,
130 'name' => $this->mName,
131 'dir' => $this->mDir,
132 ] + $attribs );
133 }
134
142 protected function getInputWidget( $params ) {
143 return new OOUI\SelectFileInputWidget( $params );
144 }
145
150 protected function shouldInfuseOOUI() {
151 return true;
152 }
153}
File <input> field.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
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:55