df0ba6e7627e79f61e38ea3b88b73b5f8e62720c
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / FileLink.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Preprocess;
4
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Element;
7 use Drupal\bootstrap\Utility\Variables;
8 use Drupal\Core\Link;
9 use Drupal\Core\Url;
10 use Drupal\file\Entity\File;
11
12 /**
13  * Pre-processes variables for the "file_link" theme hook.
14  *
15  * @ingroup plugins_preprocess
16  *
17  * @BootstrapPreprocess("file_link",
18  *   replace = "template_preprocess_file_link"
19  * )
20  */
21 class FileLink extends PreprocessBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function preprocessVariables(Variables $variables) {
27     $options = [];
28
29     $file = ($variables['file'] instanceof File) ? $variables['file'] : File::load($variables['file']->fid);
30     $url = file_create_url($file->getFileUri());
31
32     $file_size = $file->getSize();
33     $mime_type = $file->getMimeType();
34
35     // Set options as per anchor format described at
36     // http://microformats.org/wiki/file-format-examples
37     $options['attributes']['type'] = "$mime_type; length=$file_size";
38
39     // Use the description as the link text if available.
40     if (empty($variables['description'])) {
41       $link_text = $file->getFilename();
42     }
43     else {
44       $link_text = $variables['description'];
45       $options['attributes']['title'] = $file->getFilename();
46     }
47
48     // Retrieve the generic mime type from core (mislabeled as "icon_class").
49     $generic_mime_type = file_icon_class($mime_type);
50
51     // Map the generic mime types to an icon and state.
52     $mime_map = [
53       'application-x-executable' => [
54         'label' => t('binary file'),
55         'icon' => 'console',
56       ],
57       'audio' => [
58         'label' => t('audio file'),
59         'icon' => 'headphones',
60       ],
61       'image' => [
62         'label' => t('image'),
63         'icon' => 'picture',
64       ],
65       'package-x-generic' => [
66         'label' => t('archive'),
67         'icon' => 'compressed',
68       ],
69       'text' => [
70         'label' => t('document'),
71         'icon' => 'file',
72       ],
73       'video' => [
74         'label' => t('video'),
75         'icon' => 'film',
76       ],
77     ];
78
79     // Retrieve the mime map array.
80     $mime = isset($mime_map[$generic_mime_type]) ? $mime_map[$generic_mime_type] : [
81       'label' => t('file'),
82       'icon' => 'file',
83       'state' => 'primary',
84     ];
85
86     // Classes to add to the file field for icons.
87     $variables->addClass([
88       'file',
89       // Add a specific class for each and every mime type.
90       'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
91       // Add a more general class for groups of well known mime types.
92       'file--' . $generic_mime_type,
93     ]);
94
95     // Set the icon for the mime type.
96     $icon = Bootstrap::glyphicon($mime['icon']);
97     $variables->icon = Element::create($icon)
98       ->addClass('text-primary')
99       ->getArray();
100
101     $options['attributes']['title'] = t('Open @mime in new window', ['@mime' => $mime['label']]);
102     $options['attributes']['target'] = '_blank';
103
104     if ($this->theme->getSetting('tooltip_enabled')) {
105       $options['attributes']['data-toggle'] = 'tooltip';
106       $options['attributes']['data-placement'] = 'bottom';
107     }
108     $variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUri($url, $options));
109
110     // Add the file size as a variable.
111     $variables->file_size = format_size($file_size);
112
113     // Preprocess attributes.
114     $this->preprocessAttributes();
115   }
116
117 }