-
Notifications
You must be signed in to change notification settings - Fork 0
/
unb_lib_theme.theme
197 lines (176 loc) · 6.36 KB
/
unb_lib_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
<?php
/**
* @file
* Functions to support theming in the UNB Libraries theme.
*/
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Finder\Finder;
const BANNER_SUBDIR = '/img/banner';
/**
* Implements template_preprocess_checkboxes().
*/
function unb_lib_theme_preprocess_checkboxes(&$variables) {
if (count($variables['element']['#options']) < 5) {
$variables['custom_inline'] = TRUE;
}
}
/**
* Implements template_preprocess_radios().
*/
function unb_lib_theme_preprocess_radios(&$variables) {
if (count($variables['element']['#options']) < 5) {
$variables['custom_inline'] = TRUE;
}
}
/**
* Implements hook_theme_preprocess_page().
*/
function unb_lib_theme_preprocess_page(&$variables) {
// Domain indicator variable.
$domain = (explode('-', \Drupal::request()->getHost()))[0];
switch ($domain) {
case 'dev':
$variables['env_indicator'] = 'Development Server';
$variables['env_indicator_bg_color'] = 'warning';
$variables['env_indicator_txt_color'] = 'black';
break;
case 'local':
$variables['env_indicator'] = 'Local Container';
$variables['env_indicator_bg_color'] = 'primary';
$variables['env_indicator_txt_color'] = 'white';
break;
default:
$variables['env_indicator'] = null;
}
// Set is_front variable if unset (LIB-118).
if (!isset($variables['is_front'])) {
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (Exception $e) {
$variables['is_front'] = FALSE;
}
}
$base_theme_path = drupal_get_path('theme', 'unb_lib_theme');
$variables['unb_lib_theme_path'] = '/' . $base_theme_path;
$active_theme_path = \Drupal::service('theme.manager')
->getActiveTheme()
->getPath();
if ($variables['is_front']) {
// Allow for special front page banners.
$banner_path = $active_theme_path . BANNER_SUBDIR . '/front';
} else {
$banner_path = $active_theme_path . BANNER_SUBDIR;
$banner_path_subdirs = [];
if (file_exists($banner_path) && is_dir($banner_path)) {
$finder = new Finder();
$dirs = $finder
->directories()
->in($banner_path)
->depth('== 0')
->exclude('front')
->exclude('disabled');
foreach ($dirs->getIterator() as $iterator) {
$banner_path_subdirs[] = $iterator->getRelativePathname();
}
$current_url = parse_url(\Drupal::request()->getRequestUri(), PHP_URL_PATH);
$url_path_segments = explode('/', $current_url);
// Remove empty array element from explode() then order path segments from last to first.
array_shift($url_path_segments);
$url_path_segments = array_reverse($url_path_segments);
// Return banner for deepest url subpath found.
foreach ($url_path_segments as $path_segment) {
if (in_array($path_segment, $banner_path_subdirs)) {
$banner_path .= '/' . $path_segment;
break;
}
}
}
}
$banner_extensions = ['gif', 'jpg', 'png'];
$banner_image = _unb_lib_theme_get_random_file($banner_path, $banner_extensions);
if ($banner_image == NULL) {
// Base theme banner fallback.
$banner_path = $base_theme_path . BANNER_SUBDIR;
$variables['banner_image'] = '/' . _unb_lib_theme_get_random_file($banner_path, $banner_extensions);
}
else {
$variables['banner_image'] = '/' . $banner_image;
}
// Allow toggling Top Header | User Account Links.
$variables['show_top_header_login'] = theme_get_setting('show_top_header_login');
$variables['show_top_header_logout'] = theme_get_setting('show_top_header_logout');
$auth_link_options = [
'attributes' => [
'class' => 'nav-link'
],
];
$login_title = [
'#markup' => '<i class="fas fa-sign-in-alt"></i> ' . t('Log in'),
];
$login_link = Drupal\Core\Link::createFromRoute($login_title, 'user.login', [], $auth_link_options);
$variables['login_link'] = $login_link;
$logout_title = [
'#markup' => '<i class="fas fa-sign-out-alt"></i> ' . t('Log out'),
];
$logout_link = Drupal\Core\Link::createFromRoute($logout_title, 'user.logout', [], $auth_link_options);
$variables['logout_link'] = $logout_link;
}
/**
* Implements hook_form_system_theme_settings_alter().
*/
function unb_lib_theme_form_system_theme_settings_alter(&$form, FormStateInterface $form_state) {
// Override Layout | Region | Top Header settings.
unset($form['layout']['region']['top_header']['bootstrap_barrio_region_clean_top_header']);
unset($form['layout']['region']['top_header']['bootstrap_barrio_region_class_top_header']);
$form['layout']['region']['top_header']['user_account'] = [
'#type' => 'details',
'#title' => t('User Account'),
'#open' => TRUE,
];
$form['layout']['region']['top_header']['user_account']['show_top_header_login'] = [
'#type' => 'checkbox',
'#title' => t('Show Login Link'),
'#default_value' => theme_get_setting('show_top_header_login'),
'#description' => t('Check this option if you\'d like to show the <strong>Login</strong> link on the right.'),
];
$form['layout']['region']['top_header']['user_account']['show_top_header_logout'] = [
'#type' => 'checkbox',
'#title' => t('Show Logout Link'),
'#default_value' => theme_get_setting('show_top_header_logout'),
'#description' => t('Check this option if you\'d like to show the <strong>Logout</strong> link on the right'),
];
// Create a section for unb lib header banner images.
$form['lib_header_banner'] = [
'#type' => 'details',
'#title' => t('Banner Settings'),
'#description' => t('Banner settings to be used for in the theme header.'),
'#weight' => 50,
'#open' => TRUE,
];
}
/**
* Searches for and returns a random file from a given folder, optionally filtered by extension(s).
*
* @param string $path
* The path of the folder to search/select a random file from.
* @param array $extensions
* Optional: An array containing a list of file extensions to filter by.
*
* @return string $random_file
* The path + filename of the randomly selected file if available, else null.
*/
function _unb_lib_theme_get_random_file($path, $extensions = ['*']) {
$files = [];
foreach ($extensions as $extension) {
foreach (glob("$path/*.$extension") as $file) {
$files[] = $file;
}
}
$random_file = null;
if ($files != null) {
shuffle($files);
$random_file = $files[0];
}
return $random_file;
}