-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilo_base_theme.theme
233 lines (216 loc) · 8.34 KB
/
ilo_base_theme.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* @file
* Theme file.
*/
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Url;
/**
* Implements hook_preprocess().
*/
function ilo_base_theme_preprocess(&$variables) {
$variables['ilo_asset_path'] = base_path() . \Drupal::service('extension.list.module')->getPath('ilo_base_theme_companion') . '/dist/assets';
$variables['current_language_id'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
$logo_languages = [
'ar' => 'ar',
'de' => 'de',
'en' => 'en',
'es' => 'es',
'fr' => 'fr',
'it' => 'it',
'ja' => 'ja',
'nl' => 'nl',
'pt' => 'pt',
'ru' => 'ru',
'tr' => 'tr',
'uk' => 'uk',
'vi' => 'vi',
'zh-hans' => 'zh'
];
$variables['ilo_logo_language'] = array_key_exists($variables['current_language_id'], $logo_languages) ? $logo_languages[$variables['current_language_id']] : 'en';
}
/**
* Implements hook_page_attachments_alter().
*/
function ilo_base_theme_page_attachments_alter(array &$attachments) {
$active_theme = \Drupal::theme()->getActiveTheme();
if ($active_theme->getName() === 'ilo_base_theme' || array_key_exists('ilo_base_theme', $active_theme->getBaseThemeExtensions())) {
$asset_path = base_path() . \Drupal::service('extension.list.module')->getPath('ilo_base_theme_companion') . '/dist/assets/favicon';
foreach ($attachments['#attached']['html_head_link'] as &$link) {
if ($link[0]['rel'] !== 'icon') {
continue;
}
$link[0]['href'] = $asset_path . '/favicon.ico';
}
$pngs = [
'16x16' => '/favicon-16x16.png',
'32x32' => '/favicon-32x32.png',
'96x96' => '/favicon-96x96.png',
'192x192' => '/android-chrome-192x192.png',
'512x512' => '/android-chrome-512x512.png',
];
foreach ($pngs as $size => $filename) {
$attachments['#attached']['html_head_link'][] = [
[
'rel' => 'icon',
'href' => $asset_path . $filename,
'size' => $size,
'type' => 'image/png',
],
];
}
$attachments['#attached']['html_head_link'][] = [
[
'rel' => 'apple-touch-icon',
'href' => $asset_path . '/apple-touch-icon.png',
],
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function ilo_base_theme_preprocess_pattern_hero(&$variables) {
// Ignore preprocess when printing a preview.
if ($variables['context']->isOfType('preview')) {
return;
}
// Get the breadcrumb for the current route.
$route_match = \Drupal::service('current_route_match');
$breadcrumb = \Drupal::service('breadcrumb')->build($route_match);
// Add links to the breadcrumb field.
/** @var \Drupal\Core\Link $link */
$variables['breadcrumb']['links'] = [];
foreach ($breadcrumb->getLinks() as $link) {
$variables['breadcrumb']['links'][] = [
'url' => $link->getUrl(),
'label' => $link->getText(),
];
}
// Make sure that the cache metadata from the breadcrumb is not lost.
CacheableMetadata::createFromObject($breadcrumb)->applyTo($variables);
}
/**
* Implements hook_preprocess_HOOK().
*/
function ilo_base_theme_preprocess_pattern_navigation(&$variables) {
// Ignore preprocess when printing a preview.
if ($variables['context']->isOfType('preview')) {
return;
}
/** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree */
$menu_link_tree = \Drupal::service('menu.link_tree');
$parameters = new MenuTreeParameters();
$parameters->setMinDepth(1);
$parameters->setMaxDepth(3);
$parameters->onlyEnabledLinks();
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $menu_link_tree->load('main', $parameters);
$tree = $menu_link_tree->transform($tree, $manipulators);
$menu = $menu_link_tree->build($tree);
// Extract cache metadata from main menu.
$cache = CacheableMetadata::createFromRenderArray($menu);
$cache->addCacheTags(['menu_link_content_list']);
// Process current menu links and pass them to the pattern primary nav field.
$variables['primarynav']['items'] = array_map(function ($item) {
return [
'label' => $item['title'],
'link' => $item['url'],
];
}, $menu['#items']);
// Process "More" expandable items and pass them to the pattern field.
if (!empty($menu['#items']['ilo_base_theme_companion.main.more']['below'])) {
$variables['subnav'] = [
'navlabel' => t("Secondary Navigation"),
'buttonlabel' => t("More"),
'mobilecloselabel' => t("Close"),
'mobilebacklabel' => t("Menu Home"),
'items' => array_map(function ($item) {
return [
'label' => $item['title'],
'link' => $item['url'],
];
}, $menu['#items']['ilo_base_theme_companion.main.more']['below']),
];
}
// Always remove the convenience "More" menu link.
unset($variables['primarynav']['items']['ilo_base_theme_companion.main.more']);
// The following code is inspired by Drupal core's language switcher block.
// @see \Drupal\language\Plugin\Block\LanguageBlock::build().
/** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
$language_manager = \Drupal::service('language_manager');
$cache->addCacheTags(['config:configurable_language_list']);
if ($language_manager->isMultilingual()) {
$route_match = \Drupal::routeMatch();
// If there is no route match, for example when creating blocks on 404 pages
// for logged-in users with big_pipe enabled using the front page instead.
if (\Drupal::service('path.matcher')->isFrontPage() || !$route_match->getRouteObject()) {
// We are skipping the route match on both 404 and front page.
// Example: If on front page, there is no route match like when creating
// blocks on 404 pages for logged-in users with big_pipe enabled, use the
// front page.
$url = Url::fromRoute('<front>');
}
else {
$url = Url::fromRouteMatch($route_match);
}
$links = $language_manager->getLanguageSwitchLinks(LanguageInterface::TYPE_INTERFACE, $url);
$variables['languagelabel'] = $language_manager->getCurrentLanguage()->getName();
$variables['languagecontextmenu'] = [
'links' => [],
];
foreach ($links->links as $language_id => $link) {
// Do not print link to current language.
if ($language_id === $variables['current_language_id']) {
continue;
}
$link['url']->setOption('language', $link['language']);
// Bubble up all cache metadata and add it to the current render array.
$url = $link['url']->toString(TRUE);
$cache->addCacheableDependency($url);
$variables['languagecontextmenu']['links'][] = [
'label' => $link['title'],
'url' => $link['url'],
];
}
}
// Add language cache context and apply it to the current render array.
$cache->addCacheContexts(['languages:' . LanguageInterface::TYPE_INTERFACE]);
$cache->applyTo($variables);
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form_element.
*/
function ilo_base_theme_theme_suggestions_form_element_alter(&$suggestions, $variables) {
if (!empty($variables['element']['#type'])) {
$suggestions[] = 'form_element__' . $variables['element']['#type'];
}
}
/**
* Implements hook_preprocess_HOOK() for form_element__password_confirm.
*/
function ilo_base_theme_preprocess_form_element__password_confirm(&$variables) {
// Add CSS classes needed for theming the password confirm widget.
$variables['attributes']['class'][] = 'password-confirm';
$variables['attributes']['class'][] = 'is-initial';
$variables['attributes']['class'][] = 'is-password-empty';
$variables['attributes']['class'][] = 'is-confirm-empty';
}
/**
* Implements hook_preprocess_HOOK() for form_element__password.
*/
function ilo_base_theme_preprocess_form_element__password(&$variables) {
if (!empty($variables['element']['#array_parents']) && in_array('pass1', $variables['element']['#array_parents'], TRUE)) {
// This is the main password form element.
$variables['attributes']['class'][] = 'password-confirm__password';
}
if (!empty($variables['element']['#array_parents']) && in_array('pass2', $variables['element']['#array_parents'], TRUE)) {
// This is the password confirm form element.
$variables['attributes']['class'][] = 'password-confirm__confirm';
}
}