-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectre.theme
217 lines (189 loc) · 5.97 KB
/
spectre.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
<?php
/**
* @file
* Functions to support theming in the spectre theme.
*/
use Drupal\block\Entity\Block;
/**
* Implements hook_page_attachments_alter().
*/
function spectre_page_attachments_alter(array &$page) {
// Force IE to use Chrome Frame if installed.
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'X-UA-Compatible',
'content' => 'IE=edge,chrome=1',
],
],
'chrome_frame',
];
// Remove image toolbar in IE.
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'ImageToolbar',
'content' => 'false',
],
],
'ie_image_toolbar',
];
// Alter viewport.
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'viewport',
'content' => 'width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no',
],
],
'viewport',
];
}
/**
* Implements theme_preprocess_container().
*/
function spectre_preprocess_container(&$variables) {
$variables['attributes']['class'][] = 'form-group';
}
/**
* Implements template_preprocess_form_element().
*/
function spectre_preprocess_form_element(&$variables) {
// Push form element type to the label element.
$form_element_type = $variables['element']['#type'];
$variables['label']['#field_type'] = $form_element_type;
// Render the form element inside the label for checkbox and radio.
if (in_array($form_element_type, ['checkbox', 'radio'])) {
$variables['label']['#children'] = $variables['children'] . '<i class="form-icon"></i>';
unset($variables['children']);
// Specific class for radio and checkbox items.
$variables['label']['#attributes']['class'][] = 'form-' . $variables['element']['#type'];
}
else {
$variables['label']['#attributes']['class'][] = 'form-label';
}
}
/**
* Implements template_preprocess_input().
*/
function spectre_preprocess_input(&$variables) {
$element = $variables['element'];
// Specifics per input types.
switch ($element['#type']) {
case 'checkbox':
case 'radio':
// Skip for now.
break;
case 'submit':
$variables['attributes']['class'][] = 'btn';
if (theme_get_setting('button.size') !== 'default') {
$variables['attributes']['class'][] = theme_get_setting('button.size');
}
if (isset($element['#button_type']) && $element['#button_type'] === 'primary') {
$variables['attributes']['class'][] = 'btn-primary';
}
break;
case 'select':
$variables['attributes']['class'][] = 'form-select';
break;
default:
$variables['attributes']['class'][] = 'form-input';
}
}
/**
* Implements hook_preprocess_textarea().
*/
function spectre_preprocess_textarea(array &$variables) {
$variables['attributes']['class'][] = 'form-input';
}
/**
* Implements hook_preprocess_image().
*/
function spectre_preprocess_image(array &$variables) {
$variables['attributes']['class'][] = 'img-responsive';
}
/**
* Implements template_preprocess_menu_local_task().
*/
function spectre_preprocess_menu_local_task(&$variables) {
$variables['attributes']['class'][] = 'tab-item';
// If is active add class.
if (!empty($variables['element']['#active'])) {
$variables['attributes']['class'][] = 'active';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function spectre_preprocess_block(&$variables) {
// Pass block region value to content so this can be used in
// spectre_theme_suggestions_menu_alter.
if (isset($variables['elements']['#id'])) {
if ($block = Block::load($variables['elements']['#id'])) {
$variables['content']['#attributes']['region'] = $block->getRegion();
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function spectre_theme_suggestions_menu_alter(&$suggestions, array $variables) {
// Provide menu template suggesstion by region block placement.
if (isset($variables['attributes']['region'])) {
$suggestions[] = 'menu__' . $variables['attributes']['region'] . '__spectre';
}
}
/**
* Implements template_preprocess_table().
*/
function spectre_preprocess_table(&$variables) {
$variables['attributes']['class'][] = 'table';
if (theme_get_setting('table.hover')) {
$variables['attributes']['class'][] = 'table-hover';
}
if (theme_get_setting('table.striped')) {
$variables['attributes']['class'][] = 'table-striped';
}
if (theme_get_setting('table.scroll')) {
$variables['attributes']['class'][] = 'table-scroll';
}
}
/**
* Implements template_preprocess_views_view_table().
*/
function spectre_preprocess_views_view_table(&$variables) {
$variables['spectre_hover'] = theme_get_setting('table.hover');
$variables['spectre_striped'] = theme_get_setting('table.striped');
$variables['spectre_scroll'] = theme_get_setting('table.scroll');
}
/**
* Implements hook_preprocess_label().
*/
function spectre_preprocess_form_element_label(&$variables) {
if (theme_get_setting('label.enable')) {
$variables['attributes']['class'][] = 'label';
if (theme_get_setting('label.style')) {
$variables['attributes']['class'][] = 'label-rounded';
}
}
}
/**
* Implements hook_library_info_alter().
*/
function spectre_library_info_alter(&$libraries, $extension) {
// If we use CDN alter links according to version.
if ($extension === 'spectre' && isset($libraries['spectre'])) {
$version = theme_get_setting('library.version');
$libraries['spectre']['css']['base'] = [
sprintf('https://cdnjs.cloudflare.com/ajax/libs/spectre.css/%s/spectre.min.css', $version) => ['external' => TRUE],
sprintf('https://cdnjs.cloudflare.com/ajax/libs/spectre.css/%s/spectre-exp.min.css', $version) => ['external' => TRUE],
sprintf('https://cdnjs.cloudflare.com/ajax/libs/spectre.css/%s/spectre-icons.min.css', $version) => ['external' => TRUE],
];
}
}