-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminisite.theme.inc
137 lines (119 loc) · 4.08 KB
/
minisite.theme.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @file
* minisite.theme.inc
*/
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\minisite\LegacyWrapper;
/**
* Implements hook_theme().
*/
function minisite_theme($existing, $type, $theme, $path) {
return [
'minisite_widget' => [
'render element' => 'element',
],
'minisite_link' => [
'variables' => [
'file' => NULL,
'asset_path' => NULL,
'description' => NULL,
'attributes' => [],
],
],
];
}
/**
* Prepares variables for minisite widget templates.
*
* Default template: minisite-widget.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: A render element representing the minisite field widget.
*/
function template_preprocess_minisite_widget(array &$variables) {
$element = $variables['element'];
$variables['attributes'] = [
'class' => [
'minisite-widget',
'js-form-managed-file',
'form-managed-file',
'clearfix',
],
];
if (!empty($element['fids']['#value'])) {
$file = reset($element['#files']);
$element['file_' . $file->id()]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file->getSize()) . ')</span> ';
}
$variables['data'] = [];
foreach (Element::children($element) as $child) {
$variables['data'][$child] = $element[$child];
}
}
/**
* Prepares variables for minisite link templates.
*
* Default template: minisite-link.html.twig.
*
* @param array $variables
* An associative array containing:
* - file: A file object to which the link will be created.
* - asset_path: Path to the first asset in the minisite.
* - 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_minisite_link(array &$variables) {
$variables['attributes'] = new Attribute($variables['attributes']);
$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.
$variables['#cache']['contexts'][] = 'url.site';
$link_text = $file_entity->getFilename();
// Asset path is provided.
if (isset($variables['asset_path'])) {
// Asset path is a file.
if (LegacyWrapper::isValidUri($variables['asset_path'])) {
$url = file_create_url($variables['asset_path']);
}
// Asset path is an alias to a path.
else {
$url = Url::fromUserInput($variables['asset_path'])->toString();
}
// Use the description as the link text if available.
if (!empty($variables['description'])) {
$link_text = $variables['description'];
}
}
// Falling back to the archive file.
else {
$url = file_create_url($file_entity->getFileUri());
$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();
// Classes to add to the file field for icons.
$classes = [
'file',
// Add a specific class for each and every mime type.
'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
// Add a more general class for groups of well known MIME types.
'file--' . file_icon_class($mime_type),
];
$variables['attributes']->addClass($classes);
}
// Set link title.
$options['attributes']['title'] = $link_text;
$variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUserInput($url, $options));
}