file_download.module in File Download 8
.
File
file_download.moduleView source
<?php
/**
* @file
* @file
* @file
* .*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\file\Entity\File;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_help().
*/
function file_download_help($route_name, RouteMatchInterface $arg) {
switch ($route_name) {
case 'help.page.file_download':
$output = file_get_contents(drupal_get_path('module', 'file_download') . '/README.md');
return \Drupal::moduleHandler()
->moduleExists('markdown') ? Xss::filterAdmin(\Drupal::moduleHandler()
->invoke('markdown', 'filter', [
'process',
0,
-1,
$output,
])) : '<h3>file_download README</h3><pre>' . Html::escape($output) . '</pre>';
}
}
/**
* .
*
* Prepares variables for file link templates.
*
* Default template: file-link.html.twig.
*
* @param array $variables
* An associative array containing:
* - file: A file object to which the link will be created.
* - icon_directory: (optional) A path to a directory of icons to be used for
* files. Defaults to the value of the "icon.directory" variable.
* - description: A description to be displayed instead of the filename.
* - attributes: An associative array of attributes to be placed in the a tag.
*/
function template_preprocess_download_file_link(&$variables) {
$file = $variables['file'];
$options = [];
$file_entity = $file instanceof File ? $file : File::load($file->fid);
// @todo Wrap in file_url_transform_relative(). This is currently
// impossible. As a work-around, we currently add the 'url.site' cache context.
// To ensure different file URLs are generated for different sites in a.
// Multisite setup, including HTTP and HTTPS versions of the same site.
// Fix in https://www.drupal.org/node/2646744.
$url = file_create_url($file_entity
->getFileUri());
$variables['#cache']['contexts'][] = 'url.site';
$mime_type = $file
->getMimeType();
// Set options as per anchor format described at.
// http://microformats.org/wiki/file-format-examples
$options['attributes']['type'] = $mime_type . '; length=' . $file
->getSize();
// Use the title as the link text if available.
if ($variables['title'] === NULL) {
$link_text = $file_entity
->getFilename();
}
else {
$link_text = $variables['title'];
$options['attributes']['title'] = $file_entity
->getFilename();
}
// Classes to add to the file field for icons.
// Add a specific class for each and every mime type.
$classes = [
'file',
// Add a more general class for groups of well known MIME types.
'file--mime-' . strtr($mime_type, [
'/' => '-',
'.' => '-',
]),
'file--' . file_icon_class($mime_type),
];
// Set file classes to the options array.
$variables['attributes'] = new Attribute($variables['attributes']);
$variables['attributes']
->addClass($classes);
$uri = $file_entity
->getFileUri();
$parts = explode('://', $uri);
$url = 'internal:/file-download/download/' . $parts[0] . '/' . $file_entity
->id();
$variables['link'] = [
'#type' => 'link',
'#title' => Markup::create($link_text),
'#url' => Url::fromUri($url, $options),
'#allowed_tags' => [
'span',
],
];
}
/**
* Implements hook_theme().
*/
function file_download_theme() {
// From file.module.
return [
'download_file_link' => [
'variables' => [
'file' => NULL,
'title' => NULL,
'description' => NULL,
'size' => NULL,
'raw_size' => NULL,
'attributes' => [],
],
],
'download_file_title' => [
'variables' => [
'title' => NULL,
'attributes' => [],
],
],
];
}
/*
* Implements hook_download_token_info().
*/
function file_download_token_info() {
// Core tokens for nodes.
$file['type'] = [
'name' => t("File type"),
'description' => t("The shortened version of the MIME definition e.g. pdf."),
];
return [
'tokens' => [
'file' => $file,
],
];
}
/*
* Implements hook_download_tokens().
*/
function file_download_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$replacements = [];
if ($type == 'file' && !empty($data['file'])) {
/** @var \Drupal\node\NodeInterface $node */
$file = $data['file'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the node.
case 'type':
$parts = explode('/', $file
->getMimeType());
$replacements[$original] = $parts[1];
break;
}
}
}
return $replacements;
}
Functions
Name | Description |
---|---|
file_download_help | Implements hook_help(). |
file_download_theme | Implements hook_theme(). |
file_download_tokens | |
file_download_token_info | |
template_preprocess_download_file_link | . |