MediaWiki master
HTMLFileField.php
Go to the documentation of this file.
1<?php
2
4
7use OOUI\Widget;
8
23 protected $mPlaceholder = '';
25 protected $mAccept = null;
26
28 protected $mMultiple;
29
38 public function __construct( $params ) {
39 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
40 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
41 }
42
43 parent::__construct( $params );
44
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'];
49 }
50
51 $this->mAccept = $params['accept'] ?? null;
52 $this->mMultiple = !empty( $params['multiple'] );
53 }
54
58 public function loadDataFromRequest( $request ) {
59 return $request->getUpload( $this->mName )->getName();
60 }
61
66 public function getInputHTML( $value ) {
67 $attribs = [
68 'id' => $this->mID,
69 'name' => $this->mName,
70 'dir' => $this->mDir,
71 ] + $this->getTooltipAndAccessKey();
72
73 if ( $this->mClass !== '' ) {
74 $attribs['class'] = $this->mClass;
75 }
76 if ( $this->mAccept ) {
77 $attribs['accept'] = implode( ',', $this->mAccept );
78 }
79 if ( $this->mMultiple ) {
80 $attribs['multiple'] = '';
81 }
82 // Note: Placeholders are not supported by native file inputs
83
84 $allowedParams = [
85 'title',
86 'tabindex',
87 'disabled',
88 'required',
89 'autofocus',
90 'readonly',
91 ];
92
93 $attribs += $this->getAttributes( $allowedParams );
94
95 return Html::input( $this->mName, $value ?? '', 'file', $attribs );
96 }
97
102 public function getInputOOUI( $value ) {
103 $attribs = $this->getTooltipAndAccessKeyOOUI();
104
105 if ( $this->mClass !== '' ) {
106 $attribs['classes'] = [ $this->mClass ];
107 }
108 if ( $this->mPlaceholder !== '' ) {
109 $attribs['placeholder'] = $this->mPlaceholder;
110 }
111 if ( $this->mAccept ) {
112 $attribs['accept'] = $this->mAccept;
113 }
114 if ( $this->mMultiple ) {
115 $attribs['multiple'] = true;
116 }
117
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',
127 ];
128
129 $attribs += \OOUI\Element::configFromHtmlAttributes(
130 $this->getAttributes( $allowedParams )
131 );
132
133 return $this->getInputWidget( [
134 'id' => $this->mID,
135 'name' => $this->mName,
136 'dir' => $this->mDir,
137 ] + $attribs );
138 }
139
147 protected function getInputWidget( $params ) {
148 return new \OOUI\SelectFileInputWidget( $params );
149 }
150
155 protected function shouldInfuseOOUI() {
156 return true;
157 }
158}
159
161class_alias( HTMLFileField::class, 'HTMLFileField' );
array $params
The job parameters.
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
The parent class to generate form fields.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
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.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56