Pull merge.
[yaffs-website] / web / core / modules / file / src / Element / ManagedFile.php
1 <?php
2
3 namespace Drupal\file\Element;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\NestedArray;
8 use Drupal\Core\Ajax\AjaxResponse;
9 use Drupal\Core\Ajax\ReplaceCommand;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Render\Element;
12 use Drupal\Core\Render\Element\FormElement;
13 use Drupal\Core\Site\Settings;
14 use Drupal\Core\Url;
15 use Drupal\file\Entity\File;
16 use Symfony\Component\HttpFoundation\Request;
17
18 /**
19  * Provides an AJAX/progress aware widget for uploading and saving a file.
20  *
21  * @FormElement("managed_file")
22  */
23 class ManagedFile extends FormElement {
24
25   /**
26    * {@inheritdoc}
27    */
28   public function getInfo() {
29     $class = get_class($this);
30     return [
31       '#input' => TRUE,
32       '#process' => [
33         [$class, 'processManagedFile'],
34       ],
35       '#element_validate' => [
36         [$class, 'validateManagedFile'],
37       ],
38       '#pre_render' => [
39         [$class, 'preRenderManagedFile'],
40       ],
41       '#theme' => 'file_managed_file',
42       '#theme_wrappers' => ['form_element'],
43       '#progress_indicator' => 'throbber',
44       '#progress_message' => NULL,
45       '#upload_validators' => [],
46       '#upload_location' => NULL,
47       '#size' => 22,
48       '#multiple' => FALSE,
49       '#extended' => FALSE,
50       '#attached' => [
51         'library' => ['file/drupal.file'],
52       ],
53       '#accept' => NULL,
54     ];
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
61     // Find the current value of this field.
62     $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
63     foreach ($fids as $key => $fid) {
64       $fids[$key] = (int) $fid;
65     }
66     $force_default = FALSE;
67
68     // Process any input and save new uploads.
69     if ($input !== FALSE) {
70       $input['fids'] = $fids;
71       $return = $input;
72
73       // Uploads take priority over all other values.
74       if ($files = file_managed_file_save_upload($element, $form_state)) {
75         if ($element['#multiple']) {
76           $fids = array_merge($fids, array_keys($files));
77         }
78         else {
79           $fids = array_keys($files);
80         }
81       }
82       else {
83         // Check for #filefield_value_callback values.
84         // Because FAPI does not allow multiple #value_callback values like it
85         // does for #element_validate and #process, this fills the missing
86         // functionality to allow File fields to be extended through FAPI.
87         if (isset($element['#file_value_callbacks'])) {
88           foreach ($element['#file_value_callbacks'] as $callback) {
89             $callback($element, $input, $form_state);
90           }
91         }
92
93         // Load files if the FIDs have changed to confirm they exist.
94         if (!empty($input['fids'])) {
95           $fids = [];
96           foreach ($input['fids'] as $fid) {
97             if ($file = File::load($fid)) {
98               $fids[] = $file->id();
99               // Temporary files that belong to other users should never be
100               // allowed.
101               if ($file->isTemporary()) {
102                 if ($file->getOwnerId() != \Drupal::currentUser()->id()) {
103                   $force_default = TRUE;
104                   break;
105                 }
106                 // Since file ownership can't be determined for anonymous users,
107                 // they are not allowed to reuse temporary files at all. But
108                 // they do need to be able to reuse their own files from earlier
109                 // submissions of the same form, so to allow that, check for the
110                 // token added by $this->processManagedFile().
111                 elseif (\Drupal::currentUser()->isAnonymous()) {
112                   $token = NestedArray::getValue($form_state->getUserInput(), array_merge($element['#parents'], ['file_' . $file->id(), 'fid_token']));
113                   if ($token !== Crypt::hmacBase64('file-' . $file->id(), \Drupal::service('private_key')->get() . Settings::getHashSalt())) {
114                     $force_default = TRUE;
115                     break;
116                   }
117                 }
118               }
119             }
120           }
121           if ($force_default) {
122             $fids = [];
123           }
124         }
125       }
126     }
127
128     // If there is no input or if the default value was requested above, use the
129     // default value.
130     if ($input === FALSE || $force_default) {
131       if ($element['#extended']) {
132         $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : [];
133         $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []];
134       }
135       else {
136         $default_fids = isset($element['#default_value']) ? $element['#default_value'] : [];
137         $return = ['fids' => []];
138       }
139
140       // Confirm that the file exists when used as a default value.
141       if (!empty($default_fids)) {
142         $fids = [];
143         foreach ($default_fids as $fid) {
144           if ($file = File::load($fid)) {
145             $fids[] = $file->id();
146           }
147         }
148       }
149     }
150
151     $return['fids'] = $fids;
152     return $return;
153   }
154
155   /**
156    * #ajax callback for managed_file upload forms.
157    *
158    * This ajax callback takes care of the following things:
159    *   - Ensures that broken requests due to too big files are caught.
160    *   - Adds a class to the response to be able to highlight in the UI, that a
161    *     new file got uploaded.
162    *
163    * @param array $form
164    *   The build form.
165    * @param \Drupal\Core\Form\FormStateInterface $form_state
166    *   The form state.
167    * @param \Symfony\Component\HttpFoundation\Request $request
168    *   The current request.
169    *
170    * @return \Drupal\Core\Ajax\AjaxResponse
171    *   The ajax response of the ajax upload.
172    */
173   public static function uploadAjaxCallback(&$form, FormStateInterface &$form_state, Request $request) {
174     /** @var \Drupal\Core\Render\RendererInterface $renderer */
175     $renderer = \Drupal::service('renderer');
176
177     $form_parents = explode('/', $request->query->get('element_parents'));
178
179     // Sanitize form parents before using them.
180     $form_parents = array_filter($form_parents, [Element::class, 'child']);
181
182     // Retrieve the element to be rendered.
183     $form = NestedArray::getValue($form, $form_parents);
184
185     // Add the special AJAX class if a new file was added.
186     $current_file_count = $form_state->get('file_upload_delta_initial');
187     if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
188       $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
189     }
190     // Otherwise just add the new content class on a placeholder.
191     else {
192       $form['#suffix'] .= '<span class="ajax-new-content"></span>';
193     }
194
195     $status_messages = ['#type' => 'status_messages'];
196     $form['#prefix'] .= $renderer->renderRoot($status_messages);
197     $output = $renderer->renderRoot($form);
198
199     $response = new AjaxResponse();
200     $response->setAttachments($form['#attached']);
201
202     return $response->addCommand(new ReplaceCommand(NULL, $output));
203   }
204
205   /**
206    * Render API callback: Expands the managed_file element type.
207    *
208    * Expands the file type to include Upload and Remove buttons, as well as
209    * support for a default value.
210    */
211   public static function processManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
212
213     // This is used sometimes so let's implode it just once.
214     $parents_prefix = implode('_', $element['#parents']);
215
216     $fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : [];
217
218     // Set some default element properties.
219     $element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator'];
220     $element['#files'] = !empty($fids) ? File::loadMultiple($fids) : FALSE;
221     $element['#tree'] = TRUE;
222
223     // Generate a unique wrapper HTML ID.
224     $ajax_wrapper_id = Html::getUniqueId('ajax-wrapper');
225
226     $ajax_settings = [
227       'callback' => [get_called_class(), 'uploadAjaxCallback'],
228       'options' => [
229         'query' => [
230           'element_parents' => implode('/', $element['#array_parents']),
231         ],
232       ],
233       'wrapper' => $ajax_wrapper_id,
234       'effect' => 'fade',
235       'progress' => [
236         'type' => $element['#progress_indicator'],
237         'message' => $element['#progress_message'],
238       ],
239     ];
240
241     // Set up the buttons first since we need to check if they were clicked.
242     $element['upload_button'] = [
243       '#name' => $parents_prefix . '_upload_button',
244       '#type' => 'submit',
245       '#value' => t('Upload'),
246       '#attributes' => ['class' => ['js-hide']],
247       '#validate' => [],
248       '#submit' => ['file_managed_file_submit'],
249       '#limit_validation_errors' => [$element['#parents']],
250       '#ajax' => $ajax_settings,
251       '#weight' => -5,
252     ];
253
254     // Force the progress indicator for the remove button to be either 'none' or
255     // 'throbber', even if the upload button is using something else.
256     $ajax_settings['progress']['type'] = ($element['#progress_indicator'] == 'none') ? 'none' : 'throbber';
257     $ajax_settings['progress']['message'] = NULL;
258     $ajax_settings['effect'] = 'none';
259     $element['remove_button'] = [
260       '#name' => $parents_prefix . '_remove_button',
261       '#type' => 'submit',
262       '#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'),
263       '#validate' => [],
264       '#submit' => ['file_managed_file_submit'],
265       '#limit_validation_errors' => [$element['#parents']],
266       '#ajax' => $ajax_settings,
267       '#weight' => 1,
268     ];
269
270     $element['fids'] = [
271       '#type' => 'hidden',
272       '#value' => $fids,
273     ];
274
275     // Add progress bar support to the upload if possible.
276     if ($element['#progress_indicator'] == 'bar' && $implementation = file_progress_implementation()) {
277       $upload_progress_key = mt_rand();
278
279       if ($implementation == 'uploadprogress') {
280         $element['UPLOAD_IDENTIFIER'] = [
281           '#type' => 'hidden',
282           '#value' => $upload_progress_key,
283           '#attributes' => ['class' => ['file-progress']],
284           // Uploadprogress extension requires this field to be at the top of
285           // the form.
286           '#weight' => -20,
287         ];
288       }
289       elseif ($implementation == 'apc') {
290         $element['APC_UPLOAD_PROGRESS'] = [
291           '#type' => 'hidden',
292           '#value' => $upload_progress_key,
293           '#attributes' => ['class' => ['file-progress']],
294           // Uploadprogress extension requires this field to be at the top of
295           // the form.
296           '#weight' => -20,
297         ];
298       }
299
300       // Add the upload progress callback.
301       $element['upload_button']['#ajax']['progress']['url'] = Url::fromRoute('file.ajax_progress', ['key' => $upload_progress_key]);
302
303       // Set a custom submit event so we can modify the upload progress
304       // identifier element before the form gets submitted.
305       $element['upload_button']['#ajax']['event'] = 'fileUpload';
306     }
307
308     // The file upload field itself.
309     $element['upload'] = [
310       '#name' => 'files[' . $parents_prefix . ']',
311       '#type' => 'file',
312       '#title' => t('Choose a file'),
313       '#title_display' => 'invisible',
314       '#size' => $element['#size'],
315       '#multiple' => $element['#multiple'],
316       '#theme_wrappers' => [],
317       '#weight' => -10,
318       '#error_no_message' => TRUE,
319     ];
320     if (!empty($element['#accept'])) {
321       $element['upload']['#attributes'] = ['accept' => $element['#accept']];
322     }
323
324     if (!empty($fids) && $element['#files']) {
325       foreach ($element['#files'] as $delta => $file) {
326         $file_link = [
327           '#theme' => 'file_link',
328           '#file' => $file,
329         ];
330         if ($element['#multiple']) {
331           $element['file_' . $delta]['selected'] = [
332             '#type' => 'checkbox',
333             '#title' => \Drupal::service('renderer')->renderPlain($file_link),
334           ];
335         }
336         else {
337           $element['file_' . $delta]['filename'] = $file_link + ['#weight' => -10];
338         }
339         // Anonymous users who have uploaded a temporary file need a
340         // non-session-based token added so $this->valueCallback() can check
341         // that they have permission to use this file on subsequent submissions
342         // of the same form (for example, after an Ajax upload or form
343         // validation error).
344         if ($file->isTemporary() && \Drupal::currentUser()->isAnonymous()) {
345           $element['file_' . $delta]['fid_token'] = [
346             '#type' => 'hidden',
347             '#value' => Crypt::hmacBase64('file-' . $delta, \Drupal::service('private_key')->get() . Settings::getHashSalt()),
348           ];
349         }
350       }
351     }
352
353     // Add the extension list to the page as JavaScript settings.
354     if (isset($element['#upload_validators']['file_validate_extensions'][0])) {
355       $extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0])));
356       $element['upload']['#attached']['drupalSettings']['file']['elements']['#' . $element['#id']] = $extension_list;
357     }
358
359     // Let #id point to the file element, so the field label's 'for' corresponds
360     // with it.
361     $element['#id'] = &$element['upload']['#id'];
362
363     // Prefix and suffix used for Ajax replacement.
364     $element['#prefix'] = '<div id="' . $ajax_wrapper_id . '">';
365     $element['#suffix'] = '</div>';
366
367     return $element;
368   }
369
370   /**
371    * Render API callback: Hides display of the upload or remove controls.
372    *
373    * Upload controls are hidden when a file is already uploaded. Remove controls
374    * are hidden when there is no file attached. Controls are hidden here instead
375    * of in \Drupal\file\Element\ManagedFile::processManagedFile(), because
376    * #access for these buttons depends on the managed_file element's #value. See
377    * the documentation of \Drupal\Core\Form\FormBuilderInterface::doBuildForm()
378    * for more detailed information about the relationship between #process,
379    * #value, and #access.
380    *
381    * Because #access is set here, it affects display only and does not prevent
382    * JavaScript or other untrusted code from submitting the form as though
383    * access were enabled. The form processing functions for these elements
384    * should not assume that the buttons can't be "clicked" just because they are
385    * not displayed.
386    *
387    * @see \Drupal\file\Element\ManagedFile::processManagedFile()
388    * @see \Drupal\Core\Form\FormBuilderInterface::doBuildForm()
389    */
390   public static function preRenderManagedFile($element) {
391     // If we already have a file, we don't want to show the upload controls.
392     if (!empty($element['#value']['fids'])) {
393       if (!$element['#multiple']) {
394         $element['upload']['#access'] = FALSE;
395         $element['upload_button']['#access'] = FALSE;
396       }
397     }
398     // If we don't already have a file, there is nothing to remove.
399     else {
400       $element['remove_button']['#access'] = FALSE;
401     }
402     return $element;
403   }
404
405   /**
406    * Render API callback: Validates the managed_file element.
407    */
408   public static function validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
409     $clicked_button = end($form_state->getTriggeringElement()['#parents']);
410     if ($clicked_button != 'remove_button' && !empty($element['fids']['#value'])) {
411       $fids = $element['fids']['#value'];
412       foreach ($fids as $fid) {
413         if ($file = File::load($fid)) {
414           // If referencing an existing file, only allow if there are existing
415           // references. This prevents unmanaged files from being deleted if
416           // this item were to be deleted. When files that are no longer in use
417           // are automatically marked as temporary (now disabled by default),
418           // it is not safe to reference a permanent file without usage. Adding
419           // a usage and then later on removing it again would delete the file,
420           // but it is unknown if and where it is currently referenced. However,
421           // when files are not marked temporary (and then removed)
422           // automatically, it is safe to add and remove usages, as it would
423           // simply return to the current state.
424           // @see https://www.drupal.org/node/2891902
425           if ($file->isPermanent() && \Drupal::config('file.settings')->get('make_unused_managed_files_temporary')) {
426             $references = static::fileUsage()->listUsage($file);
427             if (empty($references)) {
428               // We expect the field name placeholder value to be wrapped in t()
429               // here, so it won't be escaped again as it's already marked safe.
430               $form_state->setError($element, t('The file used in the @name field may not be referenced.', ['@name' => $element['#title']]));
431             }
432           }
433         }
434         else {
435           // We expect the field name placeholder value to be wrapped in t()
436           // here, so it won't be escaped again as it's already marked safe.
437           $form_state->setError($element, t('The file referenced by the @name field does not exist.', ['@name' => $element['#title']]));
438         }
439       }
440     }
441
442     // Check required property based on the FID.
443     if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, ['upload_button', 'remove_button'])) {
444       // We expect the field name placeholder value to be wrapped in t()
445       // here, so it won't be escaped again as it's already marked safe.
446       $form_state->setError($element, t('@name field is required.', ['@name' => $element['#title']]));
447     }
448
449     // Consolidate the array value of this field to array of FIDs.
450     if (!$element['#extended']) {
451       $form_state->setValueForElement($element, $element['fids']['#value']);
452     }
453   }
454
455   /**
456    * Wraps the file usage service.
457    *
458    * @return \Drupal\file\FileUsage\FileUsageInterface
459    */
460   protected static function fileUsage() {
461     return \Drupal::service('file.usage');
462   }
463
464 }