5 * Defines a "managed_file" Form API field and a "file" field for Field module.
8 use Drupal\Core\Datetime\Entity\DateFormat;
9 use Drupal\Core\Field\FieldDefinitionInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Messenger\MessengerInterface;
12 use Drupal\Core\Render\BubbleableMetadata;
13 use Drupal\Core\Render\Element;
14 use Drupal\Core\Routing\RouteMatchInterface;
16 use Drupal\file\Entity\File;
17 use Drupal\file\FileInterface;
18 use Drupal\Component\Utility\NestedArray;
19 use Drupal\Component\Utility\Unicode;
20 use Drupal\Core\Entity\EntityStorageInterface;
21 use Drupal\Core\Template\Attribute;
24 * The regex pattern used when checking for insecure file types.
26 define('FILE_INSECURE_EXTENSION_REGEX', '/\.(php|pl|py|cgi|asp|js)(\.|$)/i');
28 // Load all Field module hooks for File.
29 require_once __DIR__ . '/file.field.inc';
32 * Implements hook_help().
34 function file_help($route_name, RouteMatchInterface $route_match) {
35 switch ($route_name) {
36 case 'help.page.file':
38 $output .= '<h3>' . t('About') . '</h3>';
39 $output .= '<p>' . t('The File module allows you to create fields that contain files. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":file_documentation">online documentation for the File module</a>.', [':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':file_documentation' => 'https://www.drupal.org/documentation/modules/file']) . '</p>';
40 $output .= '<h3>' . t('Uses') . '</h3>';
42 $output .= '<dt>' . t('Managing and displaying file fields') . '</dt>';
43 $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the file field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
44 $output .= '<dt>' . t('Allowing file extensions') . '</dt>';
45 $output .= '<dd>' . t('In the field settings, you can define the allowed file extensions (for example <em>pdf docx psd</em>) for the files that will be uploaded with the file field.') . '</dd>';
46 $output .= '<dt>' . t('Storing files') . '</dt>';
47 $output .= '<dd>' . t('Uploaded files can either be stored as <em>public</em> or <em>private</em>, depending on the <a href=":file-system">File system settings</a>. For more information, see the <a href=":system-help">System module help page</a>.', [':file-system' => \Drupal::url('system.file_system_settings'), ':system-help' => \Drupal::url('help.page', ['name' => 'system'])]) . '</dd>';
48 $output .= '<dt>' . t('Restricting the maximum file size') . '</dt>';
49 $output .= '<dd>' . t('The maximum file size that users can upload is limited by PHP settings of the server, but you can restrict by entering the desired value as the <em>Maximum upload size</em> setting. The maximum file size is automatically displayed to users in the help text of the file field.') . '</dd>';
50 $output .= '<dt>' . t('Displaying files and descriptions') . '<dt>';
51 $output .= '<dd>' . t('In the field settings, you can allow users to toggle whether individual files are displayed. In the display settings, you can then choose one of the following formats: <ul><li><em>Generic file</em> displays links to the files and adds icons that symbolize the file extensions. If <em>descriptions</em> are enabled and have been submitted, then the description is displayed instead of the file name.</li><li><em>URL to file</em> displays the full path to the file as plain text.</li><li><em>Table of files</em> lists links to the files and the file sizes in a table.</li><li><em>RSS enclosure</em> only displays the first file, and only in a RSS feed, formatted according to the RSS 2.0 syntax for enclosures.</li></ul> A file can still be linked to directly by its URI even if it is not displayed.') . '</dd>';
58 * Implements hook_field_widget_info_alter().
60 function file_field_widget_info_alter(array &$info) {
61 // Allows using the 'uri' widget for the 'file_uri' field type, which uses it
62 // as the default widget.
63 // @see \Drupal\file\Plugin\Field\FieldType\FileUriItem
64 $info['uri']['field_types'][] = 'file_uri';
68 * Loads file entities from the database.
70 * @param array|null $fids
71 * (optional) An array of entity IDs. If omitted or NULL, all entities are
74 * (optional) Whether to reset the internal file_load_multiple() cache.
78 * An array of file entities, indexed by fid.
80 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
81 * Use \Drupal\file\Entity\File::loadMultiple().
83 * @see hook_ENTITY_TYPE_load()
86 * @see \Drupal\Core\Entity\Query\EntityQueryInterface
88 function file_load_multiple(array $fids = NULL, $reset = FALSE) {
90 \Drupal::entityManager()->getStorage('file')->resetCache($fids);
92 return File::loadMultiple($fids);
96 * Loads a single file entity from the database.
101 * (optional) Whether to reset the internal file_load_multiple() cache.
104 * @return \Drupal\file\FileInterface|null
105 * A file entity or NULL if the file was not found.
107 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
108 * Use \Drupal\file\Entity\File::load().
110 * @see hook_ENTITY_TYPE_load()
111 * @see file_load_multiple()
113 function file_load($fid, $reset = FALSE) {
115 \Drupal::entityManager()->getStorage('file')->resetCache([$fid]);
117 return File::load($fid);
121 * Copies a file to a new location and adds a file record to the database.
123 * This function should be used when manipulating files that have records
124 * stored in the database. This is a powerful function that in many ways
125 * performs like an advanced version of copy().
126 * - Checks if $source and $destination are valid and readable/writable.
127 * - If file already exists in $destination either the call will error out,
128 * replace the file or rename the file based on the $replace parameter.
129 * - If the $source and $destination are equal, the behavior depends on the
130 * $replace parameter. FILE_EXISTS_REPLACE will error out. FILE_EXISTS_RENAME
131 * will rename the file until the $destination is unique.
132 * - Adds the new file to the files database. If the source file is a
133 * temporary file, the resulting file will also be a temporary file. See
134 * file_save_upload() for details on temporary files.
136 * @param \Drupal\file\FileInterface $source
138 * @param string $destination
139 * A string containing the destination that $source should be
140 * copied to. This must be a stream wrapper URI.
141 * @param int $replace
142 * (optional) Replace behavior when the destination file already exists.
143 * Possible values include:
144 * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
145 * the destination name exists, then its database entry will be updated. If
146 * no database entry is found, then a new one will be created.
147 * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
148 * filename is unique.
149 * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
151 * @return \Drupal\file\FileInterface|false
152 * File entity if the copy is successful, or FALSE in the event of an error.
154 * @see file_unmanaged_copy()
155 * @see hook_file_copy()
157 function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
158 if (!file_valid_uri($destination)) {
159 if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) {
160 \Drupal::logger('file')->notice('File %file (%realpath) could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
163 \Drupal::logger('file')->notice('File %file could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
165 \Drupal::messenger()->addError(t('The specified file %file could not be copied because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
169 if ($uri = file_unmanaged_copy($source->getFileUri(), $destination, $replace)) {
170 $file = $source->createDuplicate();
171 $file->setFileUri($uri);
172 $file->setFilename(drupal_basename($uri));
173 // If we are replacing an existing file re-use its database record.
174 // @todo Do not create a new entity in order to update it. See
175 // https://www.drupal.org/node/2241865.
176 if ($replace == FILE_EXISTS_REPLACE) {
177 $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
178 if (count($existing_files)) {
179 $existing = reset($existing_files);
180 $file->fid = $existing->id();
181 $file->setOriginalId($existing->id());
182 $file->setFilename($existing->getFilename());
185 // If we are renaming around an existing file (rather than a directory),
186 // use its basename for the filename.
187 elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
188 $file->setFilename(drupal_basename($destination));
193 // Inform modules that the file has been copied.
194 \Drupal::moduleHandler()->invokeAll('file_copy', [$file, $source]);
202 * Moves a file to a new location and update the file's database entry.
204 * - Checks if $source and $destination are valid and readable/writable.
205 * - Performs a file move if $source is not equal to $destination.
206 * - If file already exists in $destination either the call will error out,
207 * replace the file or rename the file based on the $replace parameter.
208 * - Adds the new file to the files database.
210 * @param \Drupal\file\FileInterface $source
212 * @param string $destination
213 * A string containing the destination that $source should be moved
214 * to. This must be a stream wrapper URI.
215 * @param int $replace
216 * (optional) The replace behavior when the destination file already exists.
217 * Possible values include:
218 * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
219 * the destination name exists then its database entry will be updated and
220 * $source->delete() called after invoking hook_file_move(). If no database
221 * entry is found, then the source files record will be updated.
222 * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
223 * filename is unique.
224 * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
226 * @return \Drupal\file\FileInterface|false
227 * Resulting file entity for success, or FALSE in the event of an error.
229 * @see file_unmanaged_move()
230 * @see hook_file_move()
232 function file_move(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
233 if (!file_valid_uri($destination)) {
234 if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) {
235 \Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
238 \Drupal::logger('file')->notice('File %file could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
240 \Drupal::messenger()->addError(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
244 if ($uri = file_unmanaged_move($source->getFileUri(), $destination, $replace)) {
245 $delete_source = FALSE;
247 $file = clone $source;
248 $file->setFileUri($uri);
249 // If we are replacing an existing file re-use its database record.
250 if ($replace == FILE_EXISTS_REPLACE) {
251 $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
252 if (count($existing_files)) {
253 $existing = reset($existing_files);
254 $delete_source = TRUE;
255 $file->fid = $existing->id();
256 $file->uuid = $existing->uuid();
259 // If we are renaming around an existing file (rather than a directory),
260 // use its basename for the filename.
261 elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
262 $file->setFilename(drupal_basename($destination));
267 // Inform modules that the file has been moved.
268 \Drupal::moduleHandler()->invokeAll('file_move', [$file, $source]);
270 // Delete the original if it's not in use elsewhere.
271 if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) {
281 * Checks that a file meets the criteria specified by the validators.
283 * After executing the validator callbacks specified hook_file_validate() will
284 * also be called to allow other modules to report errors about the file.
286 * @param \Drupal\file\FileInterface $file
288 * @param array $validators
289 * (optional) An associative array of callback functions used to validate
290 * the file. The keys are function names and the values arrays of callback
291 * parameters which will be passed in after the file entity. The functions
292 * should return an array of error messages; an empty array indicates that
293 * the file passed validation. The callback functions will be called in the
294 * order specified in the array, then the hook hook_file_validate()
295 * will be invoked so other modules can validate the new file.
298 * An array containing validation error messages.
300 * @see hook_file_validate()
302 function file_validate(FileInterface $file, $validators = []) {
303 // Call the validation functions specified by this function's caller.
305 foreach ($validators as $function => $args) {
306 if (function_exists($function)) {
307 array_unshift($args, $file);
308 $errors = array_merge($errors, call_user_func_array($function, $args));
312 // Let other modules perform validation on the new file.
313 return array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', [$file]));
317 * Checks for files with names longer than can be stored in the database.
319 * @param \Drupal\file\FileInterface $file
323 * An empty array if the file name length is smaller than the limit or an
324 * array containing an error message if it's not or is empty.
326 function file_validate_name_length(FileInterface $file) {
329 if (!$file->getFilename()) {
330 $errors[] = t("The file's name is empty. Please give a name to the file.");
332 if (strlen($file->getFilename()) > 240) {
333 $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
339 * Checks that the filename ends with an allowed extension.
341 * @param \Drupal\file\FileInterface $file
343 * @param string $extensions
344 * A string with a space separated list of allowed extensions.
347 * An empty array if the file extension is allowed or an array containing an
348 * error message if it's not.
350 * @see hook_file_validate()
352 function file_validate_extensions(FileInterface $file, $extensions) {
355 $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
356 if (!preg_match($regex, $file->getFilename())) {
357 $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => $extensions]);
363 * Checks that the file's size is below certain limits.
365 * @param \Drupal\file\FileInterface $file
367 * @param int $file_limit
368 * (optional) The maximum file size in bytes. Zero (the default) indicates
369 * that no limit should be enforced.
370 * @param int $user_limit
371 * (optional) The maximum number of bytes the user is allowed. Zero (the
372 * default) indicates that no limit should be enforced.
375 * An empty array if the file size is below limits or an array containing an
376 * error message if it's not.
378 * @see hook_file_validate()
380 function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
381 $user = \Drupal::currentUser();
384 if ($file_limit && $file->getSize() > $file_limit) {
385 $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => format_size($file->getSize()), '%maxsize' => format_size($file_limit)]);
388 // Save a query by only calling spaceUsed() when a limit is provided.
389 if ($user_limit && (\Drupal::entityManager()->getStorage('file')->spaceUsed($user->id()) + $file->getSize()) > $user_limit) {
390 $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => format_size($file->getSize()), '%quota' => format_size($user_limit)]);
397 * Checks that the file is recognized as a valid image.
399 * @param \Drupal\file\FileInterface $file
403 * An empty array if the file is a valid image or an array containing an error
404 * message if it's not.
406 * @see hook_file_validate()
408 function file_validate_is_image(FileInterface $file) {
411 $image_factory = \Drupal::service('image.factory');
412 $image = $image_factory->get($file->getFileUri());
413 if (!$image->isValid()) {
414 $supported_extensions = $image_factory->getSupportedExtensions();
415 $errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', ['%types' => implode(', ', $supported_extensions)]);
422 * Verifies that image dimensions are within the specified maximum and minimum.
424 * Non-image files will be ignored. If an image toolkit is available the image
425 * will be scaled to fit within the desired maximum dimensions.
427 * @param \Drupal\file\FileInterface $file
428 * A file entity. This function may resize the file affecting its size.
429 * @param string|int $maximum_dimensions
430 * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or
431 * '85x85'. If an image toolkit is installed, the image will be resized down
432 * to these dimensions. A value of zero (the default) indicates no restriction
433 * on size, so no resizing will be attempted.
434 * @param string|int $minimum_dimensions
435 * (optional) A string in the form WIDTHxHEIGHT. This will check that the
436 * image meets a minimum size. A value of zero (the default) indicates that
437 * there is no restriction on size.
440 * An empty array if the file meets the specified dimensions, was resized
441 * successfully to meet those requirements or is not an image. If the image
442 * does not meet the requirements or an attempt to resize it fails, an array
443 * containing the error message will be returned.
445 * @see hook_file_validate()
447 function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
450 // Check first that the file is an image.
451 $image_factory = \Drupal::service('image.factory');
452 $image = $image_factory->get($file->getFileUri());
454 if ($image->isValid()) {
456 if ($maximum_dimensions) {
457 // Check that it is smaller than the given dimensions.
458 list($width, $height) = explode('x', $maximum_dimensions);
459 if ($image->getWidth() > $width || $image->getHeight() > $height) {
460 // Try to resize the image to fit the dimensions.
461 if ($image->scale($width, $height)) {
464 if (!empty($width) && !empty($height)) {
465 $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
467 '%dimensions' => $maximum_dimensions,
468 '%new_width' => $image->getWidth(),
469 '%new_height' => $image->getHeight(),
472 elseif (empty($width)) {
473 $message = t('The image was resized to fit within the maximum allowed height of %height pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
475 '%height' => $height,
476 '%new_width' => $image->getWidth(),
477 '%new_height' => $image->getHeight(),
480 elseif (empty($height)) {
481 $message = t('The image was resized to fit within the maximum allowed width of %width pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
484 '%new_width' => $image->getWidth(),
485 '%new_height' => $image->getHeight(),
488 \Drupal::messenger()->addStatus($message);
491 $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
496 if ($minimum_dimensions) {
497 // Check that it is larger than the given dimensions.
498 list($width, $height) = explode('x', $minimum_dimensions);
499 if ($image->getWidth() < $width || $image->getHeight() < $height) {
501 $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.',
503 '%dimensions' => $minimum_dimensions,
504 '%width' => $image->getWidth(),
505 '%height' => $image->getHeight(),
509 $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.',
511 '%dimensions' => $minimum_dimensions,
512 '%width' => $image->getWidth(),
513 '%height' => $image->getHeight(),
524 * Saves a file to the specified destination and creates a database entry.
526 * @param string $data
527 * A string containing the contents of the file.
528 * @param string|null $destination
529 * (optional) A string containing the destination URI. This must be a stream
530 * wrapper URI. If no value or NULL is provided, a randomized name will be
531 * generated and the file will be saved using Drupal's default files scheme,
532 * usually "public://".
533 * @param int $replace
534 * (optional) The replace behavior when the destination file already exists.
535 * Possible values include:
536 * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
537 * the destination name exists, then its database entry will be updated. If
538 * no database entry is found, then a new one will be created.
539 * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
540 * filename is unique.
541 * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
543 * @return \Drupal\file\FileInterface|false
544 * A file entity, or FALSE on error.
546 * @see file_unmanaged_save_data()
548 function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
549 $user = \Drupal::currentUser();
551 if (empty($destination)) {
552 $destination = file_default_scheme() . '://';
554 if (!file_valid_uri($destination)) {
555 \Drupal::logger('file')->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', ['%destination' => $destination]);
556 \Drupal::messenger()->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.'));
560 if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
561 // Create a file entity.
562 $file = File::create([
564 'uid' => $user->id(),
565 'status' => FILE_STATUS_PERMANENT,
567 // If we are replacing an existing file re-use its database record.
568 // @todo Do not create a new entity in order to update it. See
569 // https://www.drupal.org/node/2241865.
570 if ($replace == FILE_EXISTS_REPLACE) {
571 $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
572 if (count($existing_files)) {
573 $existing = reset($existing_files);
574 $file->fid = $existing->id();
575 $file->setOriginalId($existing->id());
576 $file->setFilename($existing->getFilename());
579 // If we are renaming around an existing file (rather than a directory),
580 // use its basename for the filename.
581 elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
582 $file->setFilename(drupal_basename($destination));
592 * Examines a file entity and returns appropriate content headers for download.
594 * @param \Drupal\file\FileInterface $file
598 * An associative array of headers, as expected by
599 * \Symfony\Component\HttpFoundation\StreamedResponse.
601 function file_get_content_headers(FileInterface $file) {
602 $type = Unicode::mimeHeaderEncode($file->getMimeType());
605 'Content-Type' => $type,
606 'Content-Length' => $file->getSize(),
607 'Cache-Control' => 'private',
612 * Implements hook_theme().
614 function file_theme() {
618 'variables' => ['file' => NULL, 'description' => NULL, 'attributes' => []],
620 'file_managed_file' => [
621 'render element' => 'element',
624 'variables' => ['files' => [], 'attributes' => NULL],
627 'variables' => ['files' => [], 'attributes' => NULL],
630 // From file.field.inc.
631 'file_widget_multiple' => [
632 'render element' => 'element',
633 'file' => 'file.field.inc',
635 'file_upload_help' => [
636 'variables' => ['description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL],
637 'file' => 'file.field.inc',
643 * Implements hook_file_download().
645 function file_file_download($uri) {
646 // Get the file record based on the URI. If not in the database just return.
647 /** @var \Drupal\file\FileInterface[] $files */
648 $files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
650 foreach ($files as $item) {
651 // Since some database servers sometimes use a case-insensitive comparison
652 // by default, double check that the filename is an exact match.
653 if ($item->getFileUri() === $uri) {
663 // Find out if a temporary file is still used in the system.
664 if ($file->isTemporary()) {
665 $usage = \Drupal::service('file.usage')->listUsage($file);
666 if (empty($usage) && $file->getOwnerId() != \Drupal::currentUser()->id()) {
667 // Deny access to temporary files without usage that are not owned by the
668 // same user. This prevents the security issue that a private file that
669 // was protected by field permissions becomes available after its usage
670 // was removed and before it is actually deleted from the file system.
671 // Modules that depend on this behavior should make the file permanent
677 // Find out which (if any) fields of this type contain the file.
678 $references = file_get_file_references($file, NULL, EntityStorageInterface::FIELD_LOAD_CURRENT, NULL);
680 // Stop processing if there are no references in order to avoid returning
681 // headers for files controlled by other modules. Make an exception for
682 // temporary files where the host entity has not yet been saved (for example,
683 // an image preview on a node/add form) in which case, allow download by the
685 if (empty($references) && ($file->isPermanent() || $file->getOwnerId() != \Drupal::currentUser()->id())) {
689 if (!$file->access('download')) {
693 // Access is granted.
694 $headers = file_get_content_headers($file);
699 * Implements hook_cron().
701 function file_cron() {
702 $age = \Drupal::config('system.file')->get('temporary_maximum_age');
703 $file_storage = \Drupal::entityManager()->getStorage('file');
705 // Only delete temporary files if older than $age. Note that automatic cleanup
706 // is disabled if $age set to 0.
708 $fids = Drupal::entityQuery('file')
709 ->condition('status', FILE_STATUS_PERMANENT, '<>')
710 ->condition('changed', REQUEST_TIME - $age, '<')
713 $files = $file_storage->loadMultiple($fids);
714 foreach ($files as $file) {
715 $references = \Drupal::service('file.usage')->listUsage($file);
716 if (empty($references)) {
717 if (file_exists($file->getFileUri())) {
721 \Drupal::logger('file system')->error('Could not delete temporary file "%path" during garbage collection', ['%path' => $file->getFileUri()]);
725 \Drupal::logger('file system')->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', ['%path' => $file->getFileUri(), '%modules' => implode(', ', array_keys($references))]);
732 * Saves form file uploads.
734 * The files will be added to the {file_managed} table as temporary files.
735 * Temporary files are periodically cleaned. Use the 'file.usage' service to
736 * register the usage of the file which will automatically mark it as permanent.
738 * @param array $element
739 * The FAPI element whose values are being saved.
740 * @param \Drupal\Core\Form\FormStateInterface $form_state
741 * The current state of the form.
742 * @param null|int $delta
743 * (optional) The delta of the file to return the file entity.
745 * @param int $replace
746 * (optional) The replace behavior when the destination file already exists.
747 * Possible values include:
748 * - FILE_EXISTS_REPLACE: Replace the existing file.
749 * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
750 * filename is unique.
751 * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
753 * @return array|\Drupal\file\FileInterface|null|false
754 * An array of file entities or a single file entity if $delta != NULL. Each
755 * array element contains the file entity if the upload succeeded or FALSE if
756 * there was an error. Function returns NULL if no file was uploaded.
758 * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
759 * For backwards compatibility use core file upload widgets in forms.
762 * This function wraps file_save_upload() to allow correct error handling in
765 * @todo Revisit after https://www.drupal.org/node/2244513.
767 function _file_save_upload_from_form(array $element, FormStateInterface $form_state, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
768 // Get all errors set before calling this method. This will also clear them
770 $errors_before = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
772 $upload_location = isset($element['#upload_location']) ? $element['#upload_location'] : FALSE;
773 $upload_name = implode('_', $element['#parents']);
774 $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : [];
776 $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $replace);
778 // Get new errors that are generated while trying to save the upload. This
779 // will also clear them from $_SESSION.
780 $errors_new = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
781 if (!empty($errors_new)) {
783 if (count($errors_new) > 1) {
784 // Render multiple errors into a single message.
785 // This is needed because only one error per element is supported.
788 '#markup' => t('One or more files could not be uploaded.'),
791 '#theme' => 'item_list',
792 '#items' => $errors_new,
795 $error_message = \Drupal::service('renderer')->renderPlain($render_array);
798 $error_message = reset($errors_new);
801 $form_state->setError($element, $error_message);
804 // Ensure that errors set prior to calling this method are still shown to the
806 if (!empty($errors_before)) {
807 foreach ($errors_before as $error) {
808 \Drupal::messenger()->addError($error);
816 * Saves file uploads to a new location.
818 * The files will be added to the {file_managed} table as temporary files.
819 * Temporary files are periodically cleaned. Use the 'file.usage' service to
820 * register the usage of the file which will automatically mark it as permanent.
822 * Note that this function does not support correct form error handling. The
823 * file upload widgets in core do support this. It is advised to use these in
824 * any custom form, instead of calling this function.
826 * @param string $form_field_name
827 * A string that is the associative array key of the upload form element in
829 * @param array $validators
830 * (optional) An associative array of callback functions used to validate the
831 * file. See file_validate() for a full discussion of the array format.
832 * If the array is empty, it will be set up to call file_validate_extensions()
833 * with a safe list of extensions, as follows: "jpg jpeg gif png txt doc
834 * xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly
835 * set this array to ['file_validate_extensions' => '']. (Beware: this is not
836 * safe and should only be allowed for trusted users, if at all.)
837 * @param string|false $destination
838 * (optional) A string containing the URI that the file should be copied to.
839 * This must be a stream wrapper URI. If this value is omitted or set to
840 * FALSE, Drupal's temporary files scheme will be used ("temporary://").
841 * @param null|int $delta
842 * (optional) The delta of the file to return the file entity.
844 * @param int $replace
845 * (optional) The replace behavior when the destination file already exists.
846 * Possible values include:
847 * - FILE_EXISTS_REPLACE: Replace the existing file.
848 * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
849 * filename is unique.
850 * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
852 * @return array|\Drupal\file\FileInterface|null|false
853 * An array of file entities or a single file entity if $delta != NULL. Each
854 * array element contains the file entity if the upload succeeded or FALSE if
855 * there was an error. Function returns NULL if no file was uploaded.
857 * @see _file_save_upload_from_form()
859 * @todo: move this logic to a service in https://www.drupal.org/node/2244513.
861 function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
862 static $upload_cache;
864 $all_files = \Drupal::request()->files->get('files', []);
865 // Make sure there's an upload to process.
866 if (empty($all_files[$form_field_name])) {
869 $file_upload = $all_files[$form_field_name];
871 // Return cached objects without processing since the file will have
872 // already been processed and the paths in $_FILES will be invalid.
873 if (isset($upload_cache[$form_field_name])) {
875 return $upload_cache[$form_field_name][$delta];
877 return $upload_cache[$form_field_name];
880 // Prepare uploaded files info. Representation is slightly different
881 // for multiple uploads and we fix that here.
882 $uploaded_files = $file_upload;
883 if (!is_array($file_upload)) {
884 $uploaded_files = [$file_upload];
888 foreach ($uploaded_files as $i => $file_info) {
889 $files[$i] = _file_save_upload_single($file_info, $form_field_name, $validators, $destination, $replace);
892 // Add files to the cache.
893 $upload_cache[$form_field_name] = $files;
895 return isset($delta) ? $files[$delta] : $files;
899 * Saves a file upload to a new location.
901 * @param \SplFileInfo $file_info
902 * The file upload to save.
903 * @param string $form_field_name
904 * A string that is the associative array key of the upload form element in
906 * @param array $validators
907 * (optional) An associative array of callback functions used to validate the
909 * @param bool $destination
910 * (optional) A string containing the URI that the file should be copied to.
911 * @param int $replace
912 * (optional) The replace behavior when the destination file already exists.
914 * @return \Drupal\file\FileInterface|false
915 * The created file entity or FALSE if the uploaded file not saved.
917 * @throws \Drupal\Core\Entity\EntityStorageException
920 * This method should only be called from file_save_upload(). Use that method
923 * @see file_save_upload()
925 function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $validators = [], $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
926 $user = \Drupal::currentUser();
927 // Check for file upload errors and return FALSE for this file if a lower
928 // level system error occurred. For a complete list of errors:
929 // See http://php.net/manual/features.file-upload.errors.php.
930 switch ($file_info->getError()) {
931 case UPLOAD_ERR_INI_SIZE:
932 case UPLOAD_ERR_FORM_SIZE:
933 \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size())]));
936 case UPLOAD_ERR_PARTIAL:
937 case UPLOAD_ERR_NO_FILE:
938 \Drupal::messenger()->addError(t('The file %file could not be saved because the upload did not complete.', ['%file' => $file_info->getFilename()]));
942 // Final check that this is a valid upload, if it isn't, use the
943 // default error handler.
944 if (is_uploaded_file($file_info->getRealPath())) {
950 \Drupal::messenger()->addError(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $file_info->getFilename()]));
954 // Begin building file entity.
956 'uid' => $user->id(),
958 'filename' => $file_info->getClientOriginalName(),
959 'uri' => $file_info->getRealPath(),
960 'filesize' => $file_info->getSize(),
962 $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['filename']);
963 $file = File::create($values);
966 if (isset($validators['file_validate_extensions'])) {
967 if (isset($validators['file_validate_extensions'][0])) {
968 // Build the list of non-munged extensions if the caller provided them.
969 $extensions = $validators['file_validate_extensions'][0];
972 // If 'file_validate_extensions' is set and the list is empty then the
973 // caller wants to allow any extension. In this case we have to remove the
974 // validator or else it will reject all extensions.
975 unset($validators['file_validate_extensions']);
979 // No validator was provided, so add one using the default list.
980 // Build a default non-munged safe list for file_munge_filename().
981 $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
982 $validators['file_validate_extensions'] = [];
983 $validators['file_validate_extensions'][0] = $extensions;
986 if (!empty($extensions)) {
987 // Munge the filename to protect against possible malicious extension
988 // hiding within an unknown file type (ie: filename.html.foo).
989 $file->setFilename(file_munge_filename($file->getFilename(), $extensions));
992 // Rename potentially executable files, to help prevent exploits (i.e. will
993 // rename filename.php.foo and filename.php to filename.php.foo.txt and
994 // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
995 // evaluates to TRUE.
996 if (!\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match(FILE_INSECURE_EXTENSION_REGEX, $file->getFilename()) && (substr($file->getFilename(), -4) != '.txt')) {
997 $file->setMimeType('text/plain');
998 // The destination filename will also later be used to create the URI.
999 $file->setFilename($file->getFilename() . '.txt');
1000 // The .txt extension may not be in the allowed list of extensions. We have
1001 // to add it here or else the file upload will fail.
1002 if (!empty($extensions)) {
1003 $validators['file_validate_extensions'][0] .= ' txt';
1004 \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]));
1008 // If the destination is not provided, use the temporary directory.
1009 if (empty($destination)) {
1010 $destination = 'temporary://';
1013 // Assert that the destination contains a valid stream.
1014 $destination_scheme = file_uri_scheme($destination);
1015 if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
1016 \Drupal::messenger()->addError(t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]));
1020 $file->source = $form_field_name;
1021 // A file URI may already have a trailing slash or look like "public://".
1022 if (substr($destination, -1) != '/') {
1023 $destination .= '/';
1025 $file->destination = file_destination($destination . $file->getFilename(), $replace);
1026 // If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and
1027 // there's an existing file so we need to bail.
1028 if ($file->destination === FALSE) {
1029 \Drupal::messenger()->addError(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', ['%source' => $form_field_name, '%directory' => $destination]));
1033 // Add in our check of the file name length.
1034 $validators['file_validate_name_length'] = [];
1036 // Call the validation functions specified by this function's caller.
1037 $errors = file_validate($file, $validators);
1039 // Check for errors.
1040 if (!empty($errors)) {
1043 '#markup' => t('The specified file %name could not be uploaded.', ['%name' => $file->getFilename()]),
1046 '#theme' => 'item_list',
1047 '#items' => $errors,
1050 // @todo Add support for render arrays in
1051 // \Drupal\Core\Messenger\MessengerInterface::addMessage()?
1052 // @see https://www.drupal.org/node/2505497.
1053 \Drupal::messenger()->addError(\Drupal::service('renderer')->renderPlain($message));
1057 $file->setFileUri($file->destination);
1058 if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) {
1059 \Drupal::messenger()->addError(t('File upload error. Could not move uploaded file.'));
1060 \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', ['%file' => $file->getFilename(), '%destination' => $file->getFileUri()]);
1064 // Set the permissions on the new file.
1065 drupal_chmod($file->getFileUri());
1067 // If we are replacing an existing file re-use its database record.
1068 // @todo Do not create a new entity in order to update it. See
1069 // https://www.drupal.org/node/2241865.
1070 if ($replace == FILE_EXISTS_REPLACE) {
1071 $existing_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]);
1072 if (count($existing_files)) {
1073 $existing = reset($existing_files);
1074 $file->fid = $existing->id();
1075 $file->setOriginalId($existing->id());
1079 // If we made it this far it's safe to record this file in the database.
1082 // Allow an anonymous user who creates a non-public file to see it. See
1083 // \Drupal\file\FileAccessControlHandler::checkAccess().
1084 if ($user->isAnonymous() && $destination_scheme !== 'public') {
1085 $session = \Drupal::request()->getSession();
1086 $allowed_temp_files = $session->get('anonymous_allowed_file_ids', []);
1087 $allowed_temp_files[$file->id()] = $file->id();
1088 $session->set('anonymous_allowed_file_ids', $allowed_temp_files);
1094 * Determines the preferred upload progress implementation.
1096 * @return string|false
1097 * A string indicating which upload progress system is available. Either "apc"
1098 * or "uploadprogress". If neither are available, returns FALSE.
1100 function file_progress_implementation() {
1101 static $implementation;
1102 if (!isset($implementation)) {
1103 $implementation = FALSE;
1105 // We prefer the PECL extension uploadprogress because it supports multiple
1106 // simultaneous uploads. APCu only supports one at a time.
1107 if (extension_loaded('uploadprogress')) {
1108 $implementation = 'uploadprogress';
1110 elseif (version_compare(PHP_VERSION, '7', '<') && extension_loaded('apc') && ini_get('apc.rfc1867')) {
1111 $implementation = 'apc';
1114 return $implementation;
1118 * Implements hook_ENTITY_TYPE_predelete() for file entities.
1120 function file_file_predelete(File $file) {
1121 // @todo Remove references to a file that is in-use.
1125 * Implements hook_tokens().
1127 function file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
1128 $token_service = \Drupal::token();
1130 $url_options = ['absolute' => TRUE];
1131 if (isset($options['langcode'])) {
1132 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
1133 $langcode = $options['langcode'];
1141 if ($type == 'file' && !empty($data['file'])) {
1142 /** @var \Drupal\file\FileInterface $file */
1143 $file = $data['file'];
1145 foreach ($tokens as $name => $original) {
1147 // Basic keys and values.
1149 $replacements[$original] = $file->id();
1152 // Essential file data
1154 $replacements[$original] = $file->getFilename();
1158 $replacements[$original] = $file->getFileUri();
1162 $replacements[$original] = $file->getMimeType();
1166 $replacements[$original] = format_size($file->getSize());
1170 // Ideally, this would use file_url_transform_relative(), but because
1171 // tokens are also often used in e-mails, it's better to keep absolute
1172 // file URLs. The 'url.site' cache context is associated to ensure the
1173 // correct absolute URL is used in case of a multisite setup.
1174 $replacements[$original] = file_create_url($file->getFileUri());
1175 $bubbleable_metadata->addCacheContexts(['url.site']);
1178 // These tokens are default variations on the chained tokens handled below.
1180 $date_format = DateFormat::load('medium');
1181 $bubbleable_metadata->addCacheableDependency($date_format);
1182 $replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
1186 $date_format = DateFormat::load('medium');
1187 $bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
1188 $replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
1192 $owner = $file->getOwner();
1193 $bubbleable_metadata->addCacheableDependency($owner);
1194 $name = $owner->label();
1195 $replacements[$original] = $name;
1200 if ($date_tokens = $token_service->findWithPrefix($tokens, 'created')) {
1201 $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getCreatedTime()], $options, $bubbleable_metadata);
1204 if ($date_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
1205 $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getChangedTime()], $options, $bubbleable_metadata);
1208 if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getOwner()) {
1209 $replacements += $token_service->generate('user', $owner_tokens, ['user' => $file->getOwner()], $options, $bubbleable_metadata);
1213 return $replacements;
1217 * Implements hook_token_info().
1219 function file_token_info() {
1221 'name' => t("Files"),
1222 'description' => t("Tokens related to uploaded files."),
1223 'needs-data' => 'file',
1226 // File related tokens.
1228 'name' => t("File ID"),
1229 'description' => t("The unique ID of the uploaded file."),
1232 'name' => t("File name"),
1233 'description' => t("The name of the file on disk."),
1236 'name' => t("Path"),
1237 'description' => t("The location of the file relative to Drupal root."),
1240 'name' => t("MIME type"),
1241 'description' => t("The MIME type of the file."),
1244 'name' => t("File size"),
1245 'description' => t("The size of the file."),
1249 'description' => t("The web-accessible URL for the file."),
1251 $file['created'] = [
1252 'name' => t("Created"),
1253 'description' => t("The date the file created."),
1256 $file['changed'] = [
1257 'name' => t("Changed"),
1258 'description' => t("The date the file was most recently changed."),
1262 'name' => t("Owner"),
1263 'description' => t("The user who originally uploaded the file."),
1276 * Form submission handler for upload / remove buttons of managed_file elements.
1278 * @see \Drupal\file\Element\ManagedFile::processManagedFile()
1280 function file_managed_file_submit($form, FormStateInterface $form_state) {
1281 // Determine whether it was the upload or the remove button that was clicked,
1282 // and set $element to the managed_file element that contains that button.
1283 $parents = $form_state->getTriggeringElement()['#array_parents'];
1284 $button_key = array_pop($parents);
1285 $element = NestedArray::getValue($form, $parents);
1287 // No action is needed here for the upload button, because all file uploads on
1288 // the form are processed by \Drupal\file\Element\ManagedFile::valueCallback()
1289 // regardless of which button was clicked. Action is needed here for the
1290 // remove button, because we only remove a file in response to its remove
1291 // button being clicked.
1292 if ($button_key == 'remove_button') {
1293 $fids = array_keys($element['#files']);
1294 // Get files that will be removed.
1295 if ($element['#multiple']) {
1297 foreach (Element::children($element) as $name) {
1298 if (strpos($name, 'file_') === 0 && $element[$name]['selected']['#value']) {
1299 $remove_fids[] = (int) substr($name, 5);
1302 $fids = array_diff($fids, $remove_fids);
1305 // If we deal with single upload element remove the file and set
1306 // element's value to empty array (file could not be removed from
1307 // element if we don't do that).
1308 $remove_fids = $fids;
1312 foreach ($remove_fids as $fid) {
1313 // If it's a temporary file we can safely remove it immediately, otherwise
1314 // it's up to the implementing module to remove usages of files to have them
1316 if ($element['#files'][$fid] && $element['#files'][$fid]->isTemporary()) {
1317 $element['#files'][$fid]->delete();
1320 // Update both $form_state->getValues() and FormState::$input to reflect
1321 // that the file has been removed, so that the form is rebuilt correctly.
1322 // $form_state->getValues() must be updated in case additional submit
1323 // handlers run, and for form building functions that run during the
1324 // rebuild, such as when the managed_file element is part of a field widget.
1325 // FormState::$input must be updated so that
1326 // \Drupal\file\Element\ManagedFile::valueCallback() has correct information
1327 // during the rebuild.
1328 $form_state->setValueForElement($element['fids'], implode(' ', $fids));
1329 NestedArray::setValue($form_state->getUserInput(), $element['fids']['#parents'], implode(' ', $fids));
1332 // Set the form to rebuild so that $form is correctly updated in response to
1333 // processing the file removal. Since this function did not change $form_state
1334 // if the upload button was clicked, a rebuild isn't necessary in that
1335 // situation and calling $form_state->disableRedirect() would suffice.
1336 // However, we choose to always rebuild, to keep the form processing workflow
1337 // consistent between the two buttons.
1338 $form_state->setRebuild();
1342 * Saves any files that have been uploaded into a managed_file element.
1344 * @param array $element
1345 * The FAPI element whose values are being saved.
1346 * @param \Drupal\Core\Form\FormStateInterface $form_state
1347 * The current state of the form.
1349 * @return array|false
1350 * An array of file entities for each file that was saved, keyed by its file
1351 * ID. Each array element contains a file entity. Function returns FALSE if
1352 * upload directory could not be created or no files were uploaded.
1354 function file_managed_file_save_upload($element, FormStateInterface $form_state) {
1355 $upload_name = implode('_', $element['#parents']);
1356 $all_files = \Drupal::request()->files->get('files', []);
1357 if (empty($all_files[$upload_name])) {
1360 $file_upload = $all_files[$upload_name];
1362 $destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
1363 if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
1364 \Drupal::logger('file')->notice('The upload directory %directory for the file field %name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', ['%directory' => $destination, '%name' => $element['#field_name']]);
1365 $form_state->setError($element, t('The file could not be uploaded.'));
1369 // Save attached files to the database.
1370 $files_uploaded = $element['#multiple'] && count(array_filter($file_upload)) > 0;
1371 $files_uploaded |= !$element['#multiple'] && !empty($file_upload);
1372 if ($files_uploaded) {
1373 if (!$files = _file_save_upload_from_form($element, $form_state)) {
1374 \Drupal::logger('file')->notice('The file upload failed. %upload', ['%upload' => $upload_name]);
1378 // Value callback expects FIDs to be keys.
1379 $files = array_filter($files);
1380 $fids = array_map(function ($file) {
1384 return empty($files) ? [] : array_combine($fids, $files);
1391 * Prepares variables for file form widget templates.
1393 * Default template: file-managed-file.html.twig.
1395 * @param array $variables
1396 * An associative array containing:
1397 * - element: A render element representing the file.
1399 function template_preprocess_file_managed_file(&$variables) {
1400 $element = $variables['element'];
1402 $variables['attributes'] = [];
1403 if (isset($element['#id'])) {
1404 $variables['attributes']['id'] = $element['#id'];
1406 if (!empty($element['#attributes']['class'])) {
1407 $variables['attributes']['class'] = (array) $element['#attributes']['class'];
1412 * Prepares variables for file link templates.
1414 * Default template: file-link.html.twig.
1416 * @param array $variables
1417 * An associative array containing:
1418 * - file: A file object to which the link will be created.
1419 * - icon_directory: (optional) A path to a directory of icons to be used for
1420 * files. Defaults to the value of the "icon.directory" variable.
1421 * - description: A description to be displayed instead of the filename.
1422 * - attributes: An associative array of attributes to be placed in the a tag.
1424 function template_preprocess_file_link(&$variables) {
1425 $file = $variables['file'];
1428 $file_entity = ($file instanceof File) ? $file : File::load($file->fid);
1429 // @todo Wrap in file_url_transform_relative(). This is currently
1430 // impossible. As a work-around, we currently add the 'url.site' cache context
1431 // to ensure different file URLs are generated for different sites in a
1432 // multisite setup, including HTTP and HTTPS versions of the same site.
1433 // Fix in https://www.drupal.org/node/2646744.
1434 $url = file_create_url($file_entity->getFileUri());
1435 $variables['#cache']['contexts'][] = 'url.site';
1437 $mime_type = $file->getMimeType();
1438 // Set options as per anchor format described at
1439 // http://microformats.org/wiki/file-format-examples
1440 $options['attributes']['type'] = $mime_type . '; length=' . $file->getSize();
1442 // Use the description as the link text if available.
1443 if (empty($variables['description'])) {
1444 $link_text = $file_entity->getFilename();
1447 $link_text = $variables['description'];
1448 $options['attributes']['title'] = $file_entity->getFilename();
1451 // Classes to add to the file field for icons.
1454 // Add a specific class for each and every mime type.
1455 'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
1456 // Add a more general class for groups of well known MIME types.
1457 'file--' . file_icon_class($mime_type),
1460 // Set file classes to the options array.
1461 $variables['attributes'] = new Attribute($variables['attributes']);
1462 $variables['attributes']->addClass($classes);
1464 $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
1468 * Gets a class for the icon for a MIME type.
1470 * @param string $mime_type
1474 * A class associated with the file.
1476 function file_icon_class($mime_type) {
1477 // Search for a group with the files MIME type.
1478 $generic_mime = (string) file_icon_map($mime_type);
1479 if (!empty($generic_mime)) {
1480 return $generic_mime;
1483 // Use generic icons for each category that provides such icons.
1484 foreach (['audio', 'image', 'text', 'video'] as $category) {
1485 if (strpos($mime_type, $category) === 0) {
1490 // If there's no generic icon for the type the general class.
1495 * Determines the generic icon MIME package based on a file's MIME type.
1497 * @param string $mime_type
1500 * @return string|false
1501 * The generic icon MIME package expected for this file.
1503 function file_icon_map($mime_type) {
1504 switch ($mime_type) {
1505 // Word document types.
1506 case 'application/msword':
1507 case 'application/vnd.ms-word.document.macroEnabled.12':
1508 case 'application/vnd.oasis.opendocument.text':
1509 case 'application/vnd.oasis.opendocument.text-template':
1510 case 'application/vnd.oasis.opendocument.text-master':
1511 case 'application/vnd.oasis.opendocument.text-web':
1512 case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
1513 case 'application/vnd.stardivision.writer':
1514 case 'application/vnd.sun.xml.writer':
1515 case 'application/vnd.sun.xml.writer.template':
1516 case 'application/vnd.sun.xml.writer.global':
1517 case 'application/vnd.wordperfect':
1518 case 'application/x-abiword':
1519 case 'application/x-applix-word':
1520 case 'application/x-kword':
1521 case 'application/x-kword-crypt':
1522 return 'x-office-document';
1524 // Spreadsheet document types.
1525 case 'application/vnd.ms-excel':
1526 case 'application/vnd.ms-excel.sheet.macroEnabled.12':
1527 case 'application/vnd.oasis.opendocument.spreadsheet':
1528 case 'application/vnd.oasis.opendocument.spreadsheet-template':
1529 case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
1530 case 'application/vnd.stardivision.calc':
1531 case 'application/vnd.sun.xml.calc':
1532 case 'application/vnd.sun.xml.calc.template':
1533 case 'application/vnd.lotus-1-2-3':
1534 case 'application/x-applix-spreadsheet':
1535 case 'application/x-gnumeric':
1536 case 'application/x-kspread':
1537 case 'application/x-kspread-crypt':
1538 return 'x-office-spreadsheet';
1540 // Presentation document types.
1541 case 'application/vnd.ms-powerpoint':
1542 case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
1543 case 'application/vnd.oasis.opendocument.presentation':
1544 case 'application/vnd.oasis.opendocument.presentation-template':
1545 case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
1546 case 'application/vnd.stardivision.impress':
1547 case 'application/vnd.sun.xml.impress':
1548 case 'application/vnd.sun.xml.impress.template':
1549 case 'application/x-kpresenter':
1550 return 'x-office-presentation';
1552 // Compressed archive types.
1553 case 'application/zip':
1554 case 'application/x-zip':
1555 case 'application/stuffit':
1556 case 'application/x-stuffit':
1557 case 'application/x-7z-compressed':
1558 case 'application/x-ace':
1559 case 'application/x-arj':
1560 case 'application/x-bzip':
1561 case 'application/x-bzip-compressed-tar':
1562 case 'application/x-compress':
1563 case 'application/x-compressed-tar':
1564 case 'application/x-cpio-compressed':
1565 case 'application/x-deb':
1566 case 'application/x-gzip':
1567 case 'application/x-java-archive':
1568 case 'application/x-lha':
1569 case 'application/x-lhz':
1570 case 'application/x-lzop':
1571 case 'application/x-rar':
1572 case 'application/x-rpm':
1573 case 'application/x-tzo':
1574 case 'application/x-tar':
1575 case 'application/x-tarz':
1576 case 'application/x-tgz':
1577 return 'package-x-generic';
1579 // Script file types.
1580 case 'application/ecmascript':
1581 case 'application/javascript':
1582 case 'application/mathematica':
1583 case 'application/vnd.mozilla.xul+xml':
1584 case 'application/x-asp':
1585 case 'application/x-awk':
1586 case 'application/x-cgi':
1587 case 'application/x-csh':
1588 case 'application/x-m4':
1589 case 'application/x-perl':
1590 case 'application/x-php':
1591 case 'application/x-ruby':
1592 case 'application/x-shellscript':
1593 case 'text/vnd.wap.wmlscript':
1594 case 'text/x-emacs-lisp':
1595 case 'text/x-haskell':
1596 case 'text/x-literate-haskell':
1598 case 'text/x-makefile':
1599 case 'text/x-matlab':
1600 case 'text/x-python':
1603 return 'text-x-script';
1606 case 'application/xhtml+xml':
1609 // Executable types.
1610 case 'application/x-macbinary':
1611 case 'application/x-ms-dos-executable':
1612 case 'application/x-pef-executable':
1613 return 'application-x-executable';
1616 case 'application/pdf':
1617 case 'application/x-pdf':
1618 case 'applications/vnd.pdf':
1621 return 'application-pdf';
1629 * Retrieves a list of references to a file.
1631 * @param \Drupal\file\FileInterface $file
1633 * @param \Drupal\Core\Field\FieldDefinitionInterface|null $field
1634 * (optional) A field definition to be used for this check. If given,
1635 * limits the reference check to the given field. Defaults to NULL.
1637 * (optional) A constant that specifies which references to count. Use
1638 * EntityStorageInterface::FIELD_LOAD_REVISION (the default) to retrieve all
1639 * references within all revisions or
1640 * EntityStorageInterface::FIELD_LOAD_CURRENT to retrieve references only in
1641 * the current revisions of all entities that have references to this file.
1642 * @param string $field_type
1643 * (optional) The name of a field type. If given, limits the reference check
1644 * to fields of the given type. If both $field and $field_type are given but
1645 * $field is not the same type as $field_type, an empty array will be
1646 * returned. Defaults to 'file'.
1649 * A multidimensional array. The keys are field_name, entity_type,
1650 * entity_id and the value is an entity referencing this file.
1654 function file_get_file_references(FileInterface $file, FieldDefinitionInterface $field = NULL, $age = EntityStorageInterface::FIELD_LOAD_REVISION, $field_type = 'file') {
1655 $references = &drupal_static(__FUNCTION__, []);
1656 $field_columns = &drupal_static(__FUNCTION__ . ':field_columns', []);
1658 // Fill the static cache, disregard $field and $field_type for now.
1659 if (!isset($references[$file->id()][$age])) {
1660 $references[$file->id()][$age] = [];
1661 $usage_list = \Drupal::service('file.usage')->listUsage($file);
1662 $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : [];
1663 foreach ($file_usage_list as $entity_type_id => $entity_ids) {
1664 $entities = \Drupal::entityTypeManager()
1665 ->getStorage($entity_type_id)->loadMultiple(array_keys($entity_ids));
1666 foreach ($entities as $entity) {
1667 $bundle = $entity->bundle();
1668 // We need to find file fields for this entity type and bundle.
1669 if (!isset($file_fields[$entity_type_id][$bundle])) {
1670 $file_fields[$entity_type_id][$bundle] = [];
1671 // This contains the possible field names.
1672 foreach ($entity->getFieldDefinitions() as $field_name => $field_definition) {
1673 // If this is the first time this field type is seen, check
1674 // whether it references files.
1675 if (!isset($field_columns[$field_definition->getType()])) {
1676 $field_columns[$field_definition->getType()] = file_field_find_file_reference_column($field_definition);
1678 // If the field type does reference files then record it.
1679 if ($field_columns[$field_definition->getType()]) {
1680 $file_fields[$entity_type_id][$bundle][$field_name] = $field_columns[$field_definition->getType()];
1684 foreach ($file_fields[$entity_type_id][$bundle] as $field_name => $field_column) {
1685 // Iterate over the field items to find the referenced file and field
1686 // name. This will fail if the usage checked is in a non-current
1687 // revision because field items are from the current
1689 // We also iterate over all translations because a file can be linked
1690 // to a language other than the default.
1691 foreach ($entity->getTranslationLanguages() as $langcode => $language) {
1692 foreach ($entity->getTranslation($langcode)->get($field_name) as $item) {
1693 if ($file->id() == $item->{$field_column}) {
1694 $references[$file->id()][$age][$field_name][$entity_type_id][$entity->id()] = $entity;
1703 $return = $references[$file->id()][$age];
1704 // Filter the static cache down to the requested entries. The usual static
1705 // cache is very small so this will be very fast.
1706 if ($field || $field_type) {
1707 foreach ($return as $field_name => $data) {
1708 foreach (array_keys($data) as $entity_type_id) {
1709 $field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type_id);
1710 $current_field = $field_storage_definitions[$field_name];
1711 if (($field_type && $current_field->getType() != $field_type) || ($field && $field->uuid() != $current_field->uuid())) {
1712 unset($return[$field_name][$entity_type_id]);
1721 * Formats human-readable version of file status.
1723 * @param int|null $choice
1724 * (optional) An integer status code. If not set, all statuses are returned.
1727 * @return \Drupal\Core\StringTranslation\TranslatableMarkup|\Drupal\Core\StringTranslation\TranslatableMarkup[]
1728 * An array of file statuses or a specified status if $choice is set.
1730 function _views_file_status($choice = NULL) {
1732 0 => t('Temporary'),
1733 FILE_STATUS_PERMANENT => t('Permanent'),
1736 if (isset($choice)) {
1737 return isset($status[$choice]) ? $status[$choice] : t('Unknown');