5 * Hooks related to the File management system.
14 * Control access to private file downloads and specify HTTP headers.
16 * This hook allows modules to enforce permissions on file downloads whenever
17 * Drupal is handling file download, as opposed to the web server bypassing
18 * Drupal and returning the file from a public directory. Modules can also
19 * provide headers to specify information like the file's name or MIME type.
22 * The URI of the file.
24 * If the user does not have permission to access the file, return -1. If the
25 * user has permission, return an array with the appropriate headers. If the
26 * file is not controlled by the current module, the return value should be
29 * @see \Drupal\system\FileDownloadController::download()
31 function hook_file_download($uri) {
32 // Check to see if this is a config download.
33 $scheme = file_uri_scheme($uri);
34 $target = file_uri_target($uri);
35 if ($scheme == 'temporary' && $target == 'config.tar.gz') {
37 'Content-disposition' => 'attachment; filename="config.tar.gz"',
43 * Alter the URL to a file.
45 * This hook is called from file_create_url(), and is called fairly
46 * frequently (10+ times per page), depending on how many files there are in a
48 * If CSS and JS aggregation are disabled, this can become very frequently
49 * (50+ times per page) so performance is critical.
51 * This function should alter the URI, if it wants to rewrite the file URL.
54 * The URI to a file for which we need an external URL, or the path to a
57 function hook_file_url_alter(&$uri) {
58 $user = \Drupal::currentUser();
60 // User 1 will always see the local file in this example.
61 if ($user->id() == 1) {
65 $cdn1 = 'http://cdn1.example.com';
66 $cdn2 = 'http://cdn2.example.com';
67 $cdn_extensions = ['css', 'js', 'gif', 'jpg', 'jpeg', 'png'];
69 // Most CDNs don't support private file transfers without a lot of hassle,
70 // so don't support this in the common case.
71 $schemes = ['public'];
73 $scheme = file_uri_scheme($uri);
75 // Only serve shipped files and public created files from the CDN.
76 if (!$scheme || in_array($scheme, $schemes)) {
81 // Public created files.
83 $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
84 $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
87 // Clean up Windows paths.
88 $path = str_replace('\\', '/', $path);
90 // Serve files with one of the CDN extensions from CDN 1, all others from
92 $pathinfo = pathinfo($path);
93 if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], $cdn_extensions)) {
94 $uri = $cdn1 . '/' . $path;
97 $uri = $cdn2 . '/' . $path;
103 * Alter MIME type mappings used to determine MIME type from a file extension.
105 * Invoked by \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess(). It
106 * is used to allow modules to add to or modify the default mapping from
107 * \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping.
110 * An array of mimetypes correlated to the extensions that relate to them.
111 * The array has 'mimetypes' and 'extensions' elements, each of which is an
114 * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess()
115 * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping
117 function hook_file_mimetype_mapping_alter(&$mapping) {
118 // Add new MIME type 'drupal/info'.
119 $mapping['mimetypes']['example_info'] = 'drupal/info';
120 // Add new extension '.info.yml' and map it to the 'drupal/info' MIME type.
121 $mapping['extensions']['info'] = 'example_info';
122 // Override existing extension mapping for '.ogg' files.
123 $mapping['extensions']['ogg'] = 189;
127 * Alter archiver information declared by other modules.
129 * See hook_archiver_info() for a description of archivers and the archiver
130 * information structure.
133 * Archiver information to alter (return values from hook_archiver_info()).
135 function hook_archiver_info_alter(&$info) {
136 $info['tar']['extensions'][] = 'tgz';
140 * Register information about FileTransfer classes provided by a module.
142 * The FileTransfer class allows transferring files over a specific type of
143 * connection. Core provides classes for FTP and SSH. Contributed modules are
144 * free to extend the FileTransfer base class to add other connection types,
145 * and if these classes are registered via hook_filetransfer_info(), those
146 * connection types will be available to site administrators using the Update
147 * manager when they are redirected to the authorize.php script to authorize
148 * the file operations.
151 * Nested array of information about FileTransfer classes. Each key is a
152 * FileTransfer type (not human readable, used for form elements and
153 * variable names, etc), and the values are subarrays that define properties
154 * of that type. The keys in each subarray are:
155 * - 'title': Required. The human-readable name of the connection type.
156 * - 'class': Required. The name of the FileTransfer class. The constructor
157 * will always be passed the full path to the root of the site that should
158 * be used to restrict where file transfer operations can occur (the $jail)
159 * and an array of settings values returned by the settings form.
160 * - 'weight': Optional. Integer weight used for sorting connection types on
161 * the authorize.php form.
163 * @see \Drupal\Core\FileTransfer\FileTransfer
165 * @see hook_filetransfer_info_alter()
166 * @see drupal_get_filetransfer_info()
168 function hook_filetransfer_info() {
170 'title' => t('SFTP (Secure FTP)'),
171 'class' => 'Drupal\Core\FileTransfer\SFTP',
178 * Alter the FileTransfer class registry.
180 * @param array $filetransfer_info
181 * Reference to a nested array containing information about the FileTransfer
184 * @see hook_filetransfer_info()
186 function hook_filetransfer_info_alter(&$filetransfer_info) {
187 // Remove the FTP option entirely.
188 unset($filetransfer_info['ftp']);
189 // Make sure the SSH option is listed first.
190 $filetransfer_info['ssh']['weight'] = -10;
194 * @} End of "addtogroup hooks".