-
Notifications
You must be signed in to change notification settings - Fork 1
/
kalabase.theme
168 lines (150 loc) · 4.35 KB
/
kalabase.theme
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @file
* Theme customizations.
*/
/**
* Implements hook_preprocess().
*/
function kalabase_preprocess(&$vars) {
// Add a global flag in all themes to indicate it is rendering in a Drupal
// context.
$vars['drupal'] = TRUE;
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for field().
*/
function kalabase_theme_suggestions_field_alter(&$suggestions, $vars) {
$element = &$vars['element'];
// Fields that should use the kalagraphs_field template.
$kalagraphs_field = [
'body',
];
if (in_array($element['#field_name'], $kalagraphs_field)) {
$suggestions[] = 'kalagraphs_field';
}
}
/**
* Implements hook_preprocess_HOOK() for region().
*
* Customisations for regions.
*/
function kalabase_preprocess_region(&$vars) {
// Pass the site name through to the header so it can get printed in a deeper
// template.
if ($vars['region'] == 'header') {
$vars['site_name'] = \Drupal::config('system.site')->get('name');
}
}
/**
* Implements hook_preprocess_HOOK() for breadcrumb().
*
* Manipulates breadcrumb variables to match Kalastatic's.
*/
function kalabase_preprocess_breadcrumb(&$vars) {
$vars['title'] = array_pop($vars['breadcrumb'])['text'];
}
/**
* Implements hook_page_attachments().
*/
function kalabase_page_attachments_alter(array &$attachments) {
// Remove anyone else's attempt to add a favicon.
$links = $attachments['#attached']['html_head_link'];
foreach ($links as $key => $link) {
if ($link[0]['rel'] == 'shortcut icon') {
unset($attachments['#attached']['html_head_link'][$key]);
}
}
// Add favicon tags to head.
_kalabase_favicons($attachments);
}
/**
* Add favicon tags to the head.
*/
function _kalabase_favicons(array &$attachments) {
// Pull the brand color from saved settings.
$config = \Drupal::config('kalastatic.settings');
$brand_color = $config->get('kalastatic_brand_color');
// Get kalastatic.yaml settings so we can find the images.
$settings = kalastatic_get_settings('yaml');
$dest = '/' . $settings['destination'];
// Set up tags for the favicons.
$apple_touch_icon = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/apple-touch-icon.png',
'rel' => 'apple-touch-icon',
'sizes' => '180x180',
],
];
$attachments['#attached']['html_head'][] = [$apple_touch_icon, 'apple_touch_icon'];
$favicon_32x32 = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/favicon-32x32.png',
'rel' => 'icon',
'sizes' => '32x32',
'type' => 'image/png',
],
];
$attachments['#attached']['html_head'][] = [$favicon_32x32, 'favicon_32x32'];
$favicon_16x16 = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/favicon-16x16.png',
'rel' => 'icon',
'sizes' => '16x16',
'type' => 'image/png',
],
];
$attachments['#attached']['html_head'][] = [$favicon_16x16, 'favicon_16x16'];
$manifest = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/manifest.json',
'rel' => 'manifest',
],
];
$attachments['#attached']['html_head'][] = [$manifest, 'manifest'];
$mask_icon = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/safari-pinned-tab.svg',
'rel' => 'mask-icon',
'color' => $brand_color,
],
];
$attachments['#attached']['html_head'][] = [$mask_icon, 'mask-icon'];
$shortcut_icon = [
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'href' => $dest . '/images/favicon/favicon.ico',
'rel' => 'shortcut icon',
],
];
$attachments['#attached']['html_head'][] = [$shortcut_icon, 'shortcut icon'];
$msapplication_config = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'href' => $dest . '/images/favicon/browserconfig.xml',
'name' => 'msapplication-config',
],
];
$attachments['#attached']['html_head'][] = [$msapplication_config, 'msapplication_config'];
$theme_color = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'content' => $brand_color,
'name' => 'theme-color',
],
];
$attachments['#attached']['html_head'][] = [$theme_color, 'theme_color'];
}