forked from yalesites-org/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic.theme
93 lines (83 loc) · 2.75 KB
/
atomic.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
<?php
/**
* @file
* Functions to support theming.
*/
/**
* Implements hook_preprocess_region().
*/
function atomic_preprocess_region(&$variables) {
$variables['site_name'] = \Drupal::config('system.site')->get('name');
}
/**
* Implements hook_preprocess_menu().
*/
function atomic_preprocess_menu(&$variables, $hook) {
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
if ($item['url']->toString() == $current_path) {
// Set top level.
$variables['items'][$key]['is_active'] = TRUE;
}
if ($item['below']) {
foreach ($item['below'] as $secondaryKey => $secondaryItem) {
if ($secondaryItem['url']->toString() == $current_path) {
// Set secondary level.
$variables['items'][$key]['below'][$secondaryKey]['is_active'] = TRUE;
}
if ($secondaryItem['below']) {
foreach ($secondaryItem['below'] as $tertiaryKey => $tertiaryItem) {
if ($tertiaryItem['url']->toString() == $current_path) {
// Set tertiary level.
$variables['items'][$key]['below'][$secondaryKey]['below'][$tertiaryKey]['is_active'] = TRUE;
}
}
}
}
}
}
}
/**
* Implements hook_theme_suggestions_hook_alter() for containers.
*/
function atomic_theme_suggestions_container_alter(array &$suggestions, array &$variables) {
$type = '';
$name = '';
if (isset($variables['element']['#type'])) {
// Get the element type.
$type = $variables['element']['#type'];
// If type is defined, but not a generic "container" suggest the type.
if ($type !== 'container') {
$suggestions[] = 'container__' . $type;
}
if (isset($variables['element']['#name'])) {
// Get the element name.
$name = $variables['element']['#name'];
// If both type and name are defined, create a suggestion with both.
$suggestions[] = 'container__' . $type . '__' . $name;
}
}
}
/**
* Implements hook_preprocess_node().
*/
function atomic_preprocess_node(&$variables) {
if ($variables['node']->getType() == 'post') {
$date = strtotime($variables['node']->field_publish_date->first()->getValue()['value']);
$variables['date_formatted'] = \Drupal::service('date.formatter')->format($date, '', 'c');
}
}
/**
* Implements hook_preprocess_paragraph__accordion_item().
*
* Inject the heading_level based on the parent's heading presence.
*/
function atomic_preprocess_paragraph__accordion_item(&$variables) {
if ($accordion = $variables['paragraph']->getParentEntity()) {
if ($accordion->hasField('field_heading')) {
$heading = $accordion->field_heading->value;
$variables['attributes']['heading_level'] = empty($heading) ? 2 : 3;
}
}
}