Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / File.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7
8 /**
9  * Provides a form element for uploading a file.
10  *
11  * If you add this element to a form the enctype="multipart/form-data" attribute
12  * will automatically be added to the form element.
13  *
14  * Properties:
15  * - #multiple: A Boolean indicating whether multiple files may be uploaded.
16  * - #size: The size of the file input element in characters.
17  *
18  * @FormElement("file")
19  */
20 class File extends FormElement {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getInfo() {
26     $class = get_class($this);
27     return [
28       '#input' => TRUE,
29       '#multiple' => FALSE,
30       '#process' => [
31         [$class, 'processFile'],
32       ],
33       '#size' => 60,
34       '#pre_render' => [
35         [$class, 'preRenderFile'],
36       ],
37       '#theme' => 'input__file',
38       '#theme_wrappers' => ['form_element'],
39     ];
40   }
41
42   /**
43    * Processes a file upload element, make use of #multiple if present.
44    */
45   public static function processFile(&$element, FormStateInterface $form_state, &$complete_form) {
46     if ($element['#multiple']) {
47       $element['#attributes'] = ['multiple' => 'multiple'];
48       $element['#name'] .= '[]';
49     }
50     return $element;
51   }
52
53   /**
54    * Prepares a #type 'file' render element for input.html.twig.
55    *
56    * For assistance with handling the uploaded file correctly, see the API
57    * provided by file.inc.
58    *
59    * @param array $element
60    *   An associative array containing the properties of the element.
61    *   Properties used: #title, #name, #size, #description, #required,
62    *   #attributes.
63    *
64    * @return array
65    *   The $element with prepared variables ready for input.html.twig.
66    */
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']);
71
72     return $element;
73   }
74
75 }