-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminlte.theme
More file actions
138 lines (124 loc) · 4.52 KB
/
Copy pathadminlte.theme
File metadata and controls
138 lines (124 loc) · 4.52 KB
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
<?php
/**
* @file
* Functions to support theming in the AdminLTE 4 theme.
*/
declare(strict_types=1);
use Drupal\user\Entity\User;
use Drupal\Core\Url;
/**
* Returns the theme's layout settings with sane defaults.
*
* @return array
* Keyed by: default_color_mode, sidebar_dark.
*/
function _adminlte_layout_settings(): array {
return [
'default_color_mode' => theme_get_setting('default_color_mode', 'adminlte') ?? 'auto',
'sidebar_dark' => (bool) (theme_get_setting('sidebar_dark', 'adminlte') ?? TRUE),
];
}
/**
* Implements hook_preprocess_HOOK() for html.html.twig.
*/
function adminlte_preprocess_html(array &$variables): void {
$variables['default_color_mode'] = _adminlte_layout_settings()['default_color_mode'];
}
/**
* Implements hook_preprocess_HOOK() for page.html.twig.
*/
function adminlte_preprocess_page(array &$variables): void {
$settings = _adminlte_layout_settings();
$variables['sidebar_dark'] = $settings['sidebar_dark'];
}
/**
* Implements hook_preprocess_HOOK() for menu-local-task.html.twig.
*
* Adds Bootstrap nav classes so admin tabs render as AdminLTE nav-tabs.
*/
function adminlte_preprocess_menu_local_task(array &$variables): void {
$variables['attributes']['class'][] = 'nav-item';
if (!isset($variables['link']['#options']['attributes']['class'])) {
$variables['link']['#options']['attributes']['class'] = [];
}
$variables['link']['#options']['attributes']['class'][] = 'nav-link';
if (!empty($variables['is_active'])) {
$variables['link']['#options']['attributes']['class'][] = 'active';
}
}
/**
* Implements hook_preprocess_HOOK() for menu--admin.html.twig.
*
* Gives the top-level Administration sections recognisable Bootstrap icons.
*/
function adminlte_preprocess_menu__admin(array &$variables): void {
// The Administration menu's root is a single "Administration" item; promote
// its children (Content, Structure, …) to the top level of the sidebar so the
// sections appear directly instead of nested under one parent.
if (count($variables['items']) === 1) {
$root = reset($variables['items']);
if (!empty($root['url']) && $root['url'] instanceof Url
&& $root['url']->isRouted() && $root['url']->getRouteName() === 'system.admin') {
$variables['items'] = $root['below'] ?? [];
}
}
$route_icons = [
'system.admin_content' => 'bi-file-earmark-text',
'system.admin_structure' => 'bi-diagram-3',
'system.themes_page' => 'bi-palette',
'system.modules_list' => 'bi-puzzle',
'system.admin_config' => 'bi-gear',
'entity.user.collection' => 'bi-people',
'system.admin_reports' => 'bi-graph-up',
'help.main' => 'bi-question-circle',
'system.admin' => 'bi-speedometer2',
];
foreach ($variables['items'] as &$item) {
$route = '';
if (!empty($item['url']) && $item['url'] instanceof Url && $item['url']->isRouted()) {
$route = $item['url']->getRouteName();
}
if (isset($route_icons[$route])) {
$item['icon'] = $route_icons[$route];
}
}
}
/**
* Implements hook_preprocess_HOOK() for menu--account.html.twig.
*
* Turns the account menu into an AdminLTE navbar user dropdown: exposes the
* current user's name and picture, and maps each link to a Bootstrap icon.
*/
function adminlte_preprocess_menu__account(array &$variables): void {
$account = \Drupal::currentUser();
$variables['user_is_authenticated'] = $account->isAuthenticated();
$variables['user_name'] = $account->getDisplayName();
$variables['user_picture'] = NULL;
if ($account->isAuthenticated()) {
// The dropdown shows the current user's name/avatar, so vary by user.
$variables['#cache']['contexts'][] = 'user';
$user = User::load($account->id());
if ($user && $user->hasField('user_picture') && !$user->get('user_picture')->isEmpty()) {
$file = $user->get('user_picture')->entity;
if ($file) {
$variables['user_picture'] = \Drupal::service('file_url_generator')->generateString($file->getFileUri());
}
}
}
$route_icons = [
'user.page' => 'bi-person',
'entity.user.canonical' => 'bi-person',
'entity.user.edit_form' => 'bi-gear',
'user.admin_account' => 'bi-people',
'user.logout' => 'bi-box-arrow-right',
'user.login' => 'bi-box-arrow-in-right',
'user.register' => 'bi-person-plus',
];
foreach ($variables['items'] as &$item) {
$route = '';
if (!empty($item['url']) && $item['url'] instanceof Url && $item['url']->isRouted()) {
$route = $item['url']->getRouteName();
}
$item['icon'] = $route_icons[$route] ?? 'bi-dot';
}
}