This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
stanford_basic.theme
executable file
·367 lines (331 loc) · 12.1 KB
/
stanford_basic.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* @file
* Preprocess functions for Stanford Basic.
*/
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* Implements hook_page_attachments_alter().
*
* Because hook_page_attachments() is only executed in modules, not themes.
*/
function stanford_basic_page_attachments_alter(array &$attachments) {
if (\Drupal::currentUser()->isAuthenticated()) {
$attachments['#attached']['library'][] = 'stanford_basic/admin';
}
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'user.login') {
$attachments['#attached']['library'][] = 'stanford_basic/user_login';
}
// Check if dropdown menus are activated
$attachments['#attached']['drupalSettings']['stanford_basic']['nav_dropdown_enabled'] = (bool) theme_get_setting('nav_dropdown_enabled', 'stanford_basic');
}
/**
* Prepares variables for the html.html.twig template.
*/
function stanford_basic_preprocess_html(&$variables) {
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (Exception $e) {
// If the database is not yet available, set default values for these
// variables.
$variables['is_front'] = FALSE;
}
// If we're on the front page.
if (!$variables['is_front']) {
// Add unique classes for each page and website section.
$path = \Drupal::service('path.current')->getPath();
$alias = \Drupal::service('path_alias.manager')->getAliasByPath($path);
$alias = trim($alias, '/');
if (!empty($alias)) {
$name = str_replace('/', '-', $alias);
$variables['attributes']['class'][] = 'page-' . $name;
[$section,] = explode('/', $alias, 2);
if (!empty($section)) {
$variables['attributes']['class'][] = 'section-' . $section;
}
}
}
// Add cachability metadata.
$theme_name = \Drupal::theme()->getActiveTheme()->getName();
$theme_settings = \Drupal::config($theme_name . '.settings');
CacheableMetadata::createFromRenderArray($variables)
->addCacheableDependency($theme_settings)
->applyTo($variables);
// Union all theme setting variables to the html.html.twig template.
$variables += $theme_settings->getOriginal();
// The base path.
$variables['base_path'] = base_path();
// Add global google analytics tracker if the site is on Acquia.
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
$variables['add_global_ga'] = TRUE;
if (\Drupal::moduleHandler()->moduleExists('google_analytics')) {
$variables['ga_module_enabled'] = !empty(\Drupal::config('google_analytics.settings')
->get('account'));
}
}
}
/**
* Prepares variables for the field.html.twig template.
*/
function stanford_basic_preprocess_field(&$variables, $hook) {
// Make additional variables available to the template.
$variables['bundle'] = $variables['element']['#bundle'];
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['entity_type']);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['bundle']);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['field_name']);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['field_type']);
$variables['attributes']['class'][] = Html::cleanCssIdentifier('label-' . $variables['label_display']);
$first_item = isset($variables['element'][0]) ? $variables['element'][0] : NULL;
$is_paragraph = isset($first_item['#paragraph']) ? $first_item['#paragraph'] : FALSE;
$has_items = isset($variables['items']) ? count($variables['items']) : FALSE;
// Add additional information to paragraph fields.
// Bricks has a different field type and structures the array differently, so
// we need to check if its actual normal paragraph fields as well.
if ($variables['field_type'] == 'entity_reference_revisions' && $is_paragraph && $has_items) {
foreach ($variables['items'] as &$pitem) {
$paragraph_type = $pitem['content']['#paragraph']->getType();
$ptype = Html::cleanCssIdentifier("ptype-" . $paragraph_type);
if (!isset($pitem['attributes']['class'])) {
$pitem['attributes']['class'] = [];
}
$pitem['attributes']['class'][] = "paragraph-item";
$pitem['attributes']['class'][] = $ptype;
}
}
}
/**
* Implements hook_preprocess_block().
*/
function stanford_basic_preprocess_block(&$variables) {
$variables['attributes']['class'][] = Html::cleanCssIdentifier(_stanford_basic_change_characters($variables['base_plugin_id']));
$variables['attributes']['class'][] = Html::cleanCssIdentifier(_stanford_basic_change_characters($variables['derivative_plugin_id']));
}
/**
* Implements hook_preprocess_views_view().
*/
function stanford_basic_preprocess_views_view(&$variables) {
$variables['attributes']['class'][] = Html::cleanCssIdentifier('view');
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['id']);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($variables['display_id']);
}
/**
* Html::cleanCssIdentifier() doesn't remove ":" so we have to clean a little
* more.
*
* @param string $string
* String to clean.
*
* @return string
* Cleaned string.
*/
function _stanford_basic_change_characters($string) {
return isset($string) ? preg_replace("/[^a-zA-Z\d\s]/", '-', $string) : '';
}
/**
* Implements hook_preprocess_image().
*/
function stanford_basic_preprocess_image(&$vars) {
// Decorative images get the role="presentation" attribute.
if (!isset($vars['attributes']['alt'])) {
$vars['attributes']['role'] = 'presentation';
}
}
/**
* Implements hook_preprocess_patterns_overview_page().
*/
function stanford_basic_preprocess_patterns_overview_page(&$variables) {
uasort($variables['patterns'], function ($pattern_a, $pattern_b) {
return strcmp($pattern_a['label'], $pattern_b['label']);
});
}
/**
* Implements hook_preprocess_page().
*/
function stanford_basic_preprocess_page(&$vars) {
// Variant setting for the brand bar.
$bbv = theme_get_setting('brand_bar_variant');
if ($bbv !== "none") {
$vars['brand_bar_variant'] = 'su-brand-bar--' . $bbv;
}
// Prepares non node/panel pages to be in a single column.
_stanford_basic_preprocess_not_nodes($vars);
}
/**
* Only center the container if the page is not a node and not a panel.
*
* @param array $vars the page vars
*/
function _stanford_basic_preprocess_not_nodes(&$vars) {
$pbag = \Drupal::routeMatch()->getParameters();
$pkeys = $pbag->keys();
$blacklist = [
'node', // Node Types.
'page_manager_page', // Panels.
];
foreach ($blacklist as $key) {
if (in_array($key, $pkeys)) {
return;
}
}
// Do not center when using the layout builder ui.
if (
in_array('section_storage', $pkeys) &&
$pbag->get('section_storage') instanceof DefaultsSectionStorage
) {
return;
}
foreach ($vars['page']['content'] as $key => &$block) {
// Skip any non block config.
if (strpos($key, "#") === 0) {
continue;
}
// Ensure that the item is a block.
if (isset($block['#block'])) {
$block['#attributes']['class'][] = 'centered-container';
}
}
}
/**
* Pass through the lockup configuration settings.
*/
function stanford_basic_preprocess_block__system_branding_block(&$vars) {
$vars['use_logo'] = theme_get_setting('logo.use_default');
$vars['lockup'] = theme_get_setting('lockup') ?: ['option' => 'a'];
}
/**
* Implements hook_preprocess_menu().
*/
function stanford_basic_preprocess_menu(&$variables, $hook) {
// This should run for every menu.
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
// If path is current_path, add aria-current to the link.
if (parse_url($item['url']->toString(), PHP_URL_PATH) == $current_path) {
$link_options = $variables['items'][$key]['url']->getOptions();
$link_options['attributes']= [
'aria-current' => 'true',
];
$variables['items'][$key]['url']->setOptions($link_options);
$variables['items'][$key]['attributes']['aria-current'] = 'true';
}
}
// The following code should only run for the main menu.
if ($variables['menu_name'] !== "main") {
return;
}
foreach ($variables['items'] as &$item) {
$menu_item = [$item];
_stanford_basic_menu_process_submenu($menu_item, $current_path);
$item = $menu_item[0];
}
if (theme_get_setting('nav_dropdown_enabled', 'stanford_basic')) {
$variables['attributes']['class'][] = 'su-multi-menu--dropdowns';
}
}
/**
* Set active and active-trail class for sub-menus recursively.
*
* @param array $submenu
* The `$item['below']` structure from a menu array.
* @param string $current_path
* A path to match against for "on this page".
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function _stanford_basic_menu_process_submenu(&$submenu, $current_path) {
foreach ($submenu as &$item) {
$item_path = (is_string($item['url'])) ? $item['url'] : $item['url']->toString();
if ($item_path == $current_path ||
(_stanford_basic_path_is_home($item_path) && \Drupal::service('path.matcher')->isFrontPage())) {
$item['is_active'] = TRUE;
}
if ($item['url'] instanceof Url && !_stanford_basic_link_is_public($item['url'])) {
$item['unpublished'] = TRUE;
}
if ($item['in_active_trail'] && count($item['below'])) {
_stanford_basic_menu_process_submenu($item['below'], $current_path);
}
}
}
/**
* Determine if a menu path corresponds to the current home page.
*
* @param string $path
* The path given for a menu entry
*
* @return bool
* TRUE if the path corresponds to the current home page.
*/
function _stanford_basic_path_is_home(string $path) {
// account for weird paths in input
$normal_path = strtolower(trim($path));
$config = \Drupal::config('system.site');
$front_uri = $config->get('page.front');
$alias = \Drupal::service('path_alias.manager')->getAliasByPath($front_uri);
if ($normal_path == base_path() ||
$normal_path == $front_uri ||
$normal_path == $alias ||
$normal_path == '<front>') {
return TRUE;
}
return FALSE;
}
/**
* Is the given url public.
*
* @param \Drupal\Core\Url $url
* Menu link url object.
*
* @return bool
* False if the link is a node link to an unpublished link, true otherwise.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function _stanford_basic_link_is_public(Url $url) {
if ($url->isRouted() && $url->getRouteName() == 'entity.node.canonical') {
$parameters = $url->getRouteParameters();
/** @var \Drupal\node\NodeInterface $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($parameters['node']);
if ($node) {
return $node->isPublished();
}
return TRUE;
}
return TRUE;
}
/**
* Implements hook_preprocess_menu_local_task().
*/
function stanford_basic_preprocess_menu_local_task(&$variables) {
if ($variables['link']['#url']->getRouteName() == 'entity.node.version_history') {
$variables['link']['#title'] = t('Version History');
}
$variables['attributes']['class'][] = strtolower(Html::cleanCssIdentifier($variables['link']['#title']));
if ($variables['link']['#url']->getRouteName() == 'entity.node.canonical') {
$variables['link']['#title'] = t('Page Content');
$variables['attributes']['class'][] = 'page-content-label';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function stanford_basic_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if ($variables['elements']['#base_plugin_id'] == 'system_menu_block') {
if (\Drupal::entityTypeManager()->hasDefinition('taxonomy_menu')) {
$taxonomy_menu = \Drupal::entityTypeManager()
->getStorage('taxonomy_menu')
->loadByProperties(['menu' => $variables['elements']['#derivative_plugin_id']]);
if (!empty($taxonomy_menu)) {
$suggestions[] = 'block__system_menu_block__filter_by';
}
}
}
}