-
Notifications
You must be signed in to change notification settings - Fork 1
/
starterlight.theme
175 lines (160 loc) · 4.9 KB
/
starterlight.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
<?php
/**
* @file
* Functions to support theming in the starterlight theme.
*/
use Drupal\Component\Utility\Html;
/**
* Implements template_preprocess_html().
*/
function starterlight_preprocess_html(&$variables, $hook) {
// Add variables and paths needed for HTML5 and responsive support.
$variables['base_path'] = base_path();
$variables['path_to_starterlight'] = drupal_get_path('theme','starterlight');
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
// Add a class that tells us whether the page is viewed by an authenticated user or not.
$variables['attributes']['class'][] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';
// Add classes for basic layouts.
if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'layout-two-sidebars';
}
elseif (!empty($variables['page']['sidebar_first'])) {
$variables['attributes']['class'][] = 'layout-one-sidebar';
$variables['attributes']['class'][] = 'layout-sidebar-first';
}
elseif (!empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'layout-one-sidebar';
$variables['attributes']['class'][] = 'layout-sidebar-second';
}
else {
$variables['attributes']['class'][] = 'layout-no-sidebars';
}
}
/**
* Implements template_preprocess_block().
*/
function starterlight_preprocess_block(&$vars) {
$provider = $vars['configuration']['provider'];
$plugin_id = $vars['plugin_id'];
$attributes = [
'attributes' => [
'block',
'block-' . $provider,
'block-' . $plugin_id,
],
'title_attributes' => [
'block__label',
'block-' . $provider . '__title',
'block-' . $plugin_id . '__title',
],
'content_attributes' => [
'block__content',
'block-' . $provider . '__content',
'block-' . $plugin_id . '__content',
],
];
starterlight_add_classes($attributes, $vars);
}
/**
* Implements template_preprocess_node().
*/
function starterlight_preprocess_node(&$vars) {
$view_mode = $vars['elements']['#view_mode'];
$node = $vars['elements']['#node'];
$bundle = $node->bundle();
$attributes = [
'attributes' => [
'node',
'node--type-' . $bundle,
'node--view-mode-' . $view_mode,
$node->isPromoted() ? 'node--promoted' : '',
$node->isSticky() ? 'node--sticky' : '',
!$node->isPublished() ? 'node--unpublished' : '',
],
'title_attributes' => [
'node__title',
'node--type-' . $bundle . '__title',
],
'content_attributes' => [
'node__content',
'node--type-' . $bundle . '__content',
],
];
starterlight_add_classes($attributes, $vars);
$author_classes = [
'node__author',
'node--type-' . $bundle . '__author',
];
foreach($author_classes as $class) {
$vars['author_attributes']->addClass(Html::cleanCssIdentifier($class));
}
}
/**
* Implements template_preprocess_field().
*/
function starterlight_preprocess_field(&$vars) {
$attributes = [
'attributes' => [
'field',
'field--name-' . $vars['field_name'],
'field--type-' . $vars['field_type'],
'field--label-' . $vars['label_display'],
],
'title_attributes' => [
'field__label',
'field--name-' . $vars['field_name'] . '__label',
$vars['label_display'] === 'visually_hidden' ? 'visually-hidden' : '',
],
];
starterlight_add_classes($attributes, $vars);
}
/**
* Implements template_preprocess_region().
*/
function starterlight_preprocess_region(&$vars) {
$attributes = [
'attributes' => [
'region',
'region-' . $vars['region'],
],
];
starterlight_add_classes($attributes, $vars);
}
/**
* Implements template_preprocess_paragraph().
*/
function starterlight_preprocess_paragraph(&$vars) {
$paragraph = $vars['elements']['#paragraph'];
$view_mode = $vars['elements']['#view_mode'];
$attributes = [
'attributes' => [
'paragraph',
'paragraph--type--' . $paragraph->bundle(),
'paragraph--view-mode--' . $view_mode,
!$paragraph->isPublished() ? 'paragraph--unpublished' : '',
],
];
starterlight_add_classes($attributes, $vars);
}
/**
* Implements hook_suggestions_HOOK_alter.
*/
function starterlight_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__' . $variables['element']['#form_id'];
}
/**
* Apply lists of classes to their appropriate attribute arrays.
*
* @param array $attributes
* An associative array full of arrays keyed by attribute names. The arrays
* should contain a list of classes.
* @param array $vars
* An array of variables as passed by template_preprocess_HOOK functions.
*/
function starterlight_add_classes($attributes, &$vars) {
foreach($attributes as $attribute => $classlist) {
foreach($classlist as $class) {
$vars[$attribute]['class'][] = Html::cleanCssIdentifier($class);
}
}
}