Chris@0: 'attachment; filename="config.tar.gz"', Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter the URL to a file. Chris@0: * Chris@0: * This hook is called from file_create_url(), and is called fairly Chris@0: * frequently (10+ times per page), depending on how many files there are in a Chris@0: * given page. Chris@0: * If CSS and JS aggregation are disabled, this can become very frequently Chris@0: * (50+ times per page) so performance is critical. Chris@0: * Chris@0: * This function should alter the URI, if it wants to rewrite the file URL. Chris@0: * Chris@0: * @param $uri Chris@0: * The URI to a file for which we need an external URL, or the path to a Chris@0: * shipped file. Chris@0: */ Chris@0: function hook_file_url_alter(&$uri) { Chris@0: $user = \Drupal::currentUser(); Chris@0: Chris@0: // User 1 will always see the local file in this example. Chris@0: if ($user->id() == 1) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $cdn1 = 'http://cdn1.example.com'; Chris@0: $cdn2 = 'http://cdn2.example.com'; Chris@0: $cdn_extensions = ['css', 'js', 'gif', 'jpg', 'jpeg', 'png']; Chris@0: Chris@0: // Most CDNs don't support private file transfers without a lot of hassle, Chris@0: // so don't support this in the common case. Chris@0: $schemes = ['public']; Chris@0: Chris@0: $scheme = file_uri_scheme($uri); Chris@0: Chris@0: // Only serve shipped files and public created files from the CDN. Chris@0: if (!$scheme || in_array($scheme, $schemes)) { Chris@0: // Shipped files. Chris@0: if (!$scheme) { Chris@0: $path = $uri; Chris@0: } Chris@0: // Public created files. Chris@0: else { Chris@0: $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme); Chris@0: $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri); Chris@0: } Chris@0: Chris@0: // Clean up Windows paths. Chris@0: $path = str_replace('\\', '/', $path); Chris@0: Chris@0: // Serve files with one of the CDN extensions from CDN 1, all others from Chris@0: // CDN 2. Chris@0: $pathinfo = pathinfo($path); Chris@0: if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], $cdn_extensions)) { Chris@0: $uri = $cdn1 . '/' . $path; Chris@0: } Chris@0: else { Chris@0: $uri = $cdn2 . '/' . $path; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter MIME type mappings used to determine MIME type from a file extension. Chris@0: * Chris@0: * Invoked by \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess(). It Chris@0: * is used to allow modules to add to or modify the default mapping from Chris@0: * \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping. Chris@0: * Chris@0: * @param $mapping Chris@0: * An array of mimetypes correlated to the extensions that relate to them. Chris@0: * The array has 'mimetypes' and 'extensions' elements, each of which is an Chris@0: * array. Chris@0: * Chris@0: * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess() Chris@0: * @see \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::$defaultMapping Chris@0: */ Chris@0: function hook_file_mimetype_mapping_alter(&$mapping) { Chris@0: // Add new MIME type 'drupal/info'. Chris@0: $mapping['mimetypes']['example_info'] = 'drupal/info'; Chris@0: // Add new extension '.info.yml' and map it to the 'drupal/info' MIME type. Chris@0: $mapping['extensions']['info'] = 'example_info'; Chris@0: // Override existing extension mapping for '.ogg' files. Chris@0: $mapping['extensions']['ogg'] = 189; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter archiver information declared by other modules. Chris@0: * Chris@0: * See hook_archiver_info() for a description of archivers and the archiver Chris@0: * information structure. Chris@0: * Chris@0: * @param $info Chris@0: * Archiver information to alter (return values from hook_archiver_info()). Chris@0: */ Chris@0: function hook_archiver_info_alter(&$info) { Chris@0: $info['tar']['extensions'][] = 'tgz'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Register information about FileTransfer classes provided by a module. Chris@0: * Chris@0: * The FileTransfer class allows transferring files over a specific type of Chris@0: * connection. Core provides classes for FTP and SSH. Contributed modules are Chris@0: * free to extend the FileTransfer base class to add other connection types, Chris@0: * and if these classes are registered via hook_filetransfer_info(), those Chris@0: * connection types will be available to site administrators using the Update Chris@0: * manager when they are redirected to the authorize.php script to authorize Chris@0: * the file operations. Chris@0: * Chris@0: * @return array Chris@0: * Nested array of information about FileTransfer classes. Each key is a Chris@0: * FileTransfer type (not human readable, used for form elements and Chris@0: * variable names, etc), and the values are subarrays that define properties Chris@0: * of that type. The keys in each subarray are: Chris@0: * - 'title': Required. The human-readable name of the connection type. Chris@0: * - 'class': Required. The name of the FileTransfer class. The constructor Chris@0: * will always be passed the full path to the root of the site that should Chris@0: * be used to restrict where file transfer operations can occur (the $jail) Chris@0: * and an array of settings values returned by the settings form. Chris@0: * - 'weight': Optional. Integer weight used for sorting connection types on Chris@0: * the authorize.php form. Chris@0: * Chris@0: * @see \Drupal\Core\FileTransfer\FileTransfer Chris@0: * @see authorize.php Chris@0: * @see hook_filetransfer_info_alter() Chris@0: * @see drupal_get_filetransfer_info() Chris@0: */ Chris@0: function hook_filetransfer_info() { Chris@0: $info['sftp'] = [ Chris@0: 'title' => t('SFTP (Secure FTP)'), Chris@0: 'class' => 'Drupal\Core\FileTransfer\SFTP', Chris@0: 'weight' => 10, Chris@0: ]; Chris@0: return $info; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter the FileTransfer class registry. Chris@0: * Chris@0: * @param array $filetransfer_info Chris@0: * Reference to a nested array containing information about the FileTransfer Chris@0: * class registry. Chris@0: * Chris@0: * @see hook_filetransfer_info() Chris@0: */ Chris@0: function hook_filetransfer_info_alter(&$filetransfer_info) { Chris@0: // Remove the FTP option entirely. Chris@0: unset($filetransfer_info['ftp']); Chris@0: // Make sure the SSH option is listed first. Chris@0: $filetransfer_info['ssh']['weight'] = -10; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup hooks". Chris@0: */