MediaWiki master
HTMLFileField.php
Go to the documentation of this file.
1<?php
2
4
7use OOUI\Widget;
8
22 protected $mPlaceholder = '';
23 protected $mAccept = null;
24
26 protected $mMultiple;
27
36 public function __construct( $params ) {
37 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
38 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
39 }
40
41 parent::__construct( $params );
42
43 if ( isset( $params['placeholder-message'] ) ) {
44 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
45 } elseif ( isset( $params['placeholder'] ) ) {
46 $this->mPlaceholder = $params['placeholder'];
47 }
48
49 $this->mAccept = $params['accept'] ?? null;
50 $this->mMultiple = !empty( $params['multiple'] );
51 }
52
56 public function loadDataFromRequest( $request ) {
57 return $request->getUpload( $this->mName )->getName();
58 }
59
64 public function getInputHTML( $value ) {
65 $attribs = [
66 'id' => $this->mID,
67 'name' => $this->mName,
68 'dir' => $this->mDir,
69 ] + $this->getTooltipAndAccessKey();
70
71 if ( $this->mClass !== '' ) {
72 $attribs['class'] = $this->mClass;
73 }
74 if ( $this->mAccept ) {
75 $attribs['accept'] = implode( ',', $this->mAccept );
76 }
77 if ( $this->mMultiple ) {
78 $attribs['multiple'] = '';
79 }
80 // Note: Placeholders are not supported by native file inputs
81
82 $allowedParams = [
83 'title',
84 'tabindex',
85 'disabled',
86 'required',
87 'autofocus',
88 'readonly',
89 ];
90
91 $attribs += $this->getAttributes( $allowedParams );
92
93 return Html::input( $this->mName, $value ?? '', 'file', $attribs );
94 }
95
100 public function getInputOOUI( $value ) {
101 $attribs = $this->getTooltipAndAccessKeyOOUI();
102
103 if ( $this->mClass !== '' ) {
104 $attribs['classes'] = [ $this->mClass ];
105 }
106 if ( $this->mPlaceholder !== '' ) {
107 $attribs['placeholder'] = $this->mPlaceholder;
108 }
109 if ( $this->mAccept ) {
110 $attribs['accept'] = $this->mAccept;
111 }
112 if ( $this->mMultiple ) {
113 $attribs['multiple'] = true;
114 }
115
116 # @todo Enforce pattern, step, required, readonly on the server side as
117 # well
118 $allowedParams = [
119 'title',
120 'tabindex',
121 'disabled',
122 'required',
123 'autofocus',
124 'readonly',
125 ];
126
127 $attribs += \OOUI\Element::configFromHtmlAttributes(
128 $this->getAttributes( $allowedParams )
129 );
130
131 return $this->getInputWidget( [
132 'id' => $this->mID,
133 'name' => $this->mName,
134 'dir' => $this->mDir,
135 ] + $attribs );
136 }
137
145 protected function getInputWidget( $params ) {
146 return new \OOUI\SelectFileInputWidget( $params );
147 }
148
153 protected function shouldInfuseOOUI() {
154 return true;
155 }
156}
157
159class_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