3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
9 * Provides a form element for uploading a file.
11 * If you add this element to a form the enctype="multipart/form-data" attribute
12 * will automatically be added to the form element.
15 * - #multiple: A Boolean indicating whether multiple files may be uploaded.
16 * - #size: The size of the file input element in characters.
18 * @FormElement("file")
20 class File extends FormElement {
25 public function getInfo() {
26 $class = get_class($this);
31 [$class, 'processFile'],
35 [$class, 'preRenderFile'],
37 '#theme' => 'input__file',
38 '#theme_wrappers' => ['form_element'],
43 * Processes a file upload element, make use of #multiple if present.
45 public static function processFile(&$element, FormStateInterface $form_state, &$complete_form) {
46 if ($element['#multiple']) {
47 $element['#attributes'] = ['multiple' => 'multiple'];
48 $element['#name'] .= '[]';
54 * Prepares a #type 'file' render element for input.html.twig.
56 * For assistance with handling the uploaded file correctly, see the API
57 * provided by file.inc.
59 * @param array $element
60 * An associative array containing the properties of the element.
61 * Properties used: #title, #name, #size, #description, #required,
65 * The $element with prepared variables ready for input.html.twig.
67 public static function preRenderFile($element) {
68 $element['#attributes']['type'] = 'file';
69 Element::setAttributes($element, ['id', 'name', 'size']);
70 static::setAttributes($element, ['js-form-file', 'form-file']);