Version 1
[yaffs-website] / web / core / modules / file / file.field.inc
1 <?php
2
3 /**
4  * @file
5  * Field module functionality for the File module.
6  */
7
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Field\FieldFilteredMarkup;
10 use Drupal\Core\Render\Element;
11
12 /**
13  * Prepares variables for multi file form widget templates.
14  *
15  * Default template: file-widget-multiple.html.twig.
16  *
17  * @param array $variables
18  *   An associative array containing:
19  *   - element: A render element representing the widgets.
20  */
21 function template_preprocess_file_widget_multiple(&$variables) {
22   $element = $variables['element'];
23
24   // Special ID and classes for draggable tables.
25   $weight_class = $element['#id'] . '-weight';
26   $table_id = $element['#id'] . '-table';
27
28   // Build up a table of applicable fields.
29   $headers = [];
30   $headers[] = t('File information');
31   if ($element['#display_field']) {
32     $headers[] = [
33       'data' => t('Display'),
34       'class' => ['checkbox'],
35     ];
36   }
37   $headers[] = t('Weight');
38   $headers[] = t('Operations');
39
40   // Get our list of widgets in order (needed when the form comes back after
41   // preview or failed validation).
42   $widgets = [];
43   foreach (Element::children($element) as $key) {
44     $widgets[] = &$element[$key];
45   }
46   usort($widgets, '_field_multiple_value_form_sort_helper');
47
48   $rows = [];
49   foreach ($widgets as $key => &$widget) {
50     // Save the uploading row for last.
51     if (empty($widget['#files'])) {
52       $widget['#title'] = $element['#file_upload_title'];
53       $widget['#description'] = \Drupal::service('renderer')->renderPlain($element['#file_upload_description']);
54       continue;
55     }
56
57     // Delay rendering of the buttons, so that they can be rendered later in the
58     // "operations" column.
59     $operations_elements = [];
60     foreach (Element::children($widget) as $sub_key) {
61       if (isset($widget[$sub_key]['#type']) && $widget[$sub_key]['#type'] == 'submit') {
62         hide($widget[$sub_key]);
63         $operations_elements[] = &$widget[$sub_key];
64       }
65     }
66
67     // Delay rendering of the "Display" option and the weight selector, so that
68     // each can be rendered later in its own column.
69     if ($element['#display_field']) {
70       hide($widget['display']);
71     }
72     hide($widget['_weight']);
73
74     // Render everything else together in a column, without the normal wrappers.
75     $widget['#theme_wrappers'] = [];
76     $information = drupal_render($widget);
77     $display = '';
78     if ($element['#display_field']) {
79       unset($widget['display']['#title']);
80       $display = [
81         'data' => render($widget['display']),
82         'class' => ['checkbox'],
83       ];
84     }
85     $widget['_weight']['#attributes']['class'] = [$weight_class];
86     $weight = render($widget['_weight']);
87
88     // Arrange the row with all of the rendered columns.
89     $row = [];
90     $row[] = $information;
91     if ($element['#display_field']) {
92       $row[] = $display;
93     }
94     $row[] = $weight;
95
96     // Show the buttons that had previously been marked as hidden in this
97     // preprocess function. We use show() to undo the earlier hide().
98     foreach (Element::children($operations_elements) as $key) {
99       show($operations_elements[$key]);
100     }
101     $row[] = [
102       'data' => $operations_elements,
103     ];
104     $rows[] = [
105       'data' => $row,
106       'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], ['draggable']) : ['draggable'],
107     ];
108   }
109
110   $variables['table'] = [
111     '#type' => 'table',
112     '#header' => $headers,
113     '#rows' => $rows,
114     '#attributes' => [
115       'id' => $table_id,
116     ],
117     '#tabledrag' => [
118       [
119         'action' => 'order',
120         'relationship' => 'sibling',
121         'group' => $weight_class,
122       ],
123     ],
124     '#access' => !empty($rows),
125   ];
126
127   $variables['element'] = $element;
128 }
129
130 /**
131  * Prepares variables for file upload help text templates.
132  *
133  * Default template: file-upload-help.html.twig.
134  *
135  * @param array $variables
136  *   An associative array containing:
137  *   - description: The normal description for this field, specified by the
138  *     user.
139  *   - upload_validators: An array of upload validators as used in
140  *     $element['#upload_validators'].
141  */
142 function template_preprocess_file_upload_help(&$variables) {
143   $description = $variables['description'];
144   $upload_validators = $variables['upload_validators'];
145   $cardinality = $variables['cardinality'];
146
147   $descriptions = [];
148
149   if (!empty($description)) {
150     $descriptions[] = FieldFilteredMarkup::create($description);
151   }
152   if (isset($cardinality)) {
153     if ($cardinality == -1) {
154       $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
155     }
156     else {
157       $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
158     }
159   }
160   if (isset($upload_validators['file_validate_size'])) {
161     $descriptions[] = t('@size limit.', ['@size' => format_size($upload_validators['file_validate_size'][0])]);
162   }
163   if (isset($upload_validators['file_validate_extensions'])) {
164     $descriptions[] = t('Allowed types: @extensions.', ['@extensions' => $upload_validators['file_validate_extensions'][0]]);
165   }
166
167   if (isset($upload_validators['file_validate_image_resolution'])) {
168     $max = $upload_validators['file_validate_image_resolution'][0];
169     $min = $upload_validators['file_validate_image_resolution'][1];
170     if ($min && $max && $min == $max) {
171       $descriptions[] = t('Images must be exactly <strong>@size</strong> pixels.', ['@size' => $max]);
172     }
173     elseif ($min && $max) {
174       $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels. Images larger than <strong>@max</strong> pixels will be resized.', ['@min' => $min, '@max' => $max]);
175     }
176     elseif ($min) {
177       $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', ['@min' => $min]);
178     }
179     elseif ($max) {
180       $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', ['@max' => $max]);
181     }
182   }
183
184   $variables['descriptions'] = $descriptions;
185 }
186
187 /**
188  * Determine whether a field references files stored in {file_managed}.
189  *
190  * @param \Drupal\Core\Field\FieldDefinitionInterface $field
191  *   A field definition.
192  *
193  * @return bool
194  *   The field column if the field references {file_managed}.fid, typically
195  *   fid, FALSE if it does not.
196  */
197 function file_field_find_file_reference_column(FieldDefinitionInterface $field) {
198   $schema = $field->getFieldStorageDefinition()->getSchema();
199   foreach ($schema['foreign keys'] as $data) {
200     if ($data['table'] == 'file_managed') {
201       foreach ($data['columns'] as $field_column => $column) {
202         if ($column == 'fid') {
203           return $field_column;
204         }
205       }
206     }
207   }
208   return FALSE;
209 }