-
Notifications
You must be signed in to change notification settings - Fork 6
/
fett.theme
323 lines (291 loc) · 9.11 KB
/
fett.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
<?php
/**
* @file
* Functions to support theming in Fett.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Render\Element;
include_once 'includes/fett.offcanvas.php';
include_once 'includes/fett.mobile_menu.php';
/**
* Implements hook_theme().
*/
function fett_theme() {
$module_path = drupal_get_path('theme', 'fett');
return []
+ fett_offcanvas_theme($module_path)
+ fett_mobile_menu_theme($module_path);
}
/**
* Implements hook_theme_registry_alter().
*/
function fett_theme_registry_alter(&$theme_registry) {
// Add another page preprocess function that will get called after all child
// theme preprocess functions.
$theme_registry['page']['preprocess functions'][] = 'fett_preprocess_page_last';
}
/**
* Implements hook_library_info_alter().
*/
function fett_library_info_alter(&$libraries, $extension) {
// dsm($libraries);
$active_theme = \Drupal::theme()->getActiveTheme();
$theme = $active_theme->getName();
if ($extension == 'fett' && $theme !== 'fett') {
// Use subtheme assets if they exists.
$theme_path = $active_theme->getPath();
foreach ($libraries as &$library) {
if (!empty($library['override_enable'])) {
// Allow child theme to override Fett css.
if (!empty($library['css'])) {
foreach ($library['css'] as $type => $files) {
foreach ($files as $file => $data) {
$new_path = str_replace('fett', $theme, $theme_path . '/' . $file);
if (file_exists($new_path)) {
$library['css'][$type]['/' . $new_path] = $data;
unset($library['css'][$type][$file]);
}
}
}
}
// Allow child theme to override Fett js.
if (!empty($library['js'])) {
foreach ($library['js'] as $file => $data) {
$new_path = $theme_path . '/' . $file;
if (file_exists($new_path)) {
$library['js']['/' . $new_path] = $data;
unset($library['js'][$file]);
}
}
}
}
}
}
}
/**
* Alter attachments (typically assets) to a page before it is rendered.
*
* Use this hook when you want to remove or alter attachments on the page, or
* add attachments to the page that depend on another module's attachments (this
* hook runs after hook_page_attachments().
*
* @param array &$page
* An empty renderable array representing the page.
*
* @see hook_page_attachments_alter()
*/
function fett_page_attachments_alter(array &$page) {
$active_theme = \Drupal::theme()->getActiveTheme();
$theme = $active_theme->getName();
$theme_path = $active_theme->getPath();
$config = \Drupal::config($theme . '.settings')->get('settings');
// Process extension settings.
if (isset($config['enable_extensions']) && $config['enable_extensions'] === 1) {
// Attach poly-fills to support IE8.
if (isset($config['enable_legacy_browsers']) && $config['enable_legacy_browsers'] === 1) {
if (isset($config['legacy_browser_polyfills']) && $config['legacy_browser_polyfills'] === 1) {
$page['#attached']['library'][] = 'fett/fett.html5shiv';
$page['#attached']['library'][] = 'fett/fett.respond';
$page['#attached']['library'][] = 'fett/fett.selectivizr';
}
}
}
// Optimize mobile viewport.
$page['#attached']['html_head'][] = array(
array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'width=device-width',
),
),
'mobile_viewport',
);
// Force IE to use Chrome Frame if installed.
$page['#attached']['html_head'][] = array(
array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'content' => 'ie=edge, chrome=1',
'http-equiv' => 'x-ua-compatible',
),
),
'chrome_frame',
);
// Remove image toolbar in IE.
$page['#attached']['html_head'][] = array(
array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'ImageToolbar',
'content' => 'false',
),
),
'ie_image_toolbar',
);
}
/**
* Implements hook_form_alter().
*/
function fett_form_alter(&$form, &$form_state, $form_id) {
// Sexy submit buttons.
if (!empty($form['actions']) && !empty($form['actions']['submit'])) {
$form['actions']['submit']['#attributes']['class'][] = 'primary';
$form['actions']['submit']['#attributes']['class'][] = 'button';
$form['actions']['submit']['#attributes']['class'][] = 'radius';
}
}
/**
* Implements hook_preprocess_page().
*/
function fett_preprocess_page(&$vars) {
$active_theme = \Drupal::theme()->getActiveTheme();
$theme = $active_theme->getName();
$config = \Drupal::config($theme . '.settings')->get('settings');
$vars['logo'] = [
'#theme' => 'image',
'#uri' => $vars['base_path'] . $vars['directory'] . '/logo.svg',
'#attributes' => [
'class' => [
'site-logo',
],
],
];
$vars['prefix'] = isset($vars['prefix']) ? $vars['prefix'] : [];
$vars['suffix'] = isset($vars['suffix']) ? $vars['suffix'] : [];
$vars['attributes'] = isset($vars['attributes']) ? $vars['attributes'] : [];
foreach (Element::children($vars['page']) as $region_id) {
foreach (Element::children($vars['page'][$region_id]) as $block_id) {
if ($block_id == $theme . '_messages') {
$vars['messages'] = $vars['page'][$region_id][$block_id];
unset($vars['page'][$region_id][$block_id]);
}
}
}
// Mobile menu ops.
fett_mobile_menu_preprocess_page($vars, $config);
}
/**
* Implements hook_preprocess_page().
*/
function fett_preprocess_node(&$vars) {
$vars['tag'] = 'article';
$vars['title_element'] = 'h2';
$vars['link_title'] = TRUE;
$vars['content_element'] = '';
$vars['content_attributes'] = [];
}
/**
* Implements template_preprocess_node.
*/
function fett_preprocess_field(&$vars) {
$vars['tag'] = 'div';
$vars['content_element'] = '';
}
/**
* Last page preprocessor.
*
* This page preprocess function will be called after all other preprocessing
* has been completed.
*/
function fett_preprocess_page_last(&$vars) {
$theme = \Drupal::theme()->getActiveTheme()->getName();
$config = \Drupal::config($theme . '.settings')->get('settings');
// Offcanvas ops.
fett_offcanvas_preprocess_page($vars, $config);
}
/**
* Implements hook_preprocess_eck_entity().
*/
function fett_preprocess_eck_entity(&$vars) {
$vars['attributes']['class'][] = Html::getClass($vars['entity_type']);
$vars['attributes']['class'][] = Html::getClass($vars['bundle']);
$vars['attributes']['class'][] = Html::getClass($vars['elements']['#view_mode']);
}
/**
* Implements hook_preprocess_block().
*/
function fett_preprocess_block(&$vars) {
$theme = \Drupal::theme()->getActiveTheme()->getName();
$config = \Drupal::config($theme . '.settings')->get('settings');
fett_mobile_menu_preprocess_block($vars, $config);
// Remove classes for blocks we don't need wrapper. block.html.twig will
// not wrap if attributes are empty.
switch ($vars['plugin_id']) {
case 'system_main_block':
$vars['attributes'] = [];
break;
}
}
/**
* Implments template_preprocess_views_view().
*/
function fett_preprocess_views_view(&$vars) {
$view = $vars['view'];
if ($view->ajaxEnabled() == FALSE) {
// Empty the dom_id if ajax is disabled.
$vars['dom_id'] = '';
}
else {
// Add an ajax class as well as the dom_id.
$vars['attributes']['class'][] = 'ajax';
}
}
/**
* Implments template_preprocess_views_view().
*/
function fett_preprocess_container(&$vars) {
$element = $vars['element'];
// Remove classes for containers we don't need wrapper. container.html.twig
// will not wrap if attributes are empty.
switch ($element['#type']) {
case 'view':
$vars['attributes'] = [];
break;
}
}
/**
* Implements template_preprocess_file().
*/
function fett_preprocess_file_link(&$vars) {
$vars['attributes']['class'][] = 'icon';
}
/**
* Implements hook_preprocess_menu().
*/
function fett_preprocess_menu(&$vars) {
// Ignore admin menus.
if (isset($vars['theme_hook_original']) && $vars['theme_hook_original'] == 'menu__toolbar__admin') {
return;
}
_fett_preprocess_menu_alter_items($vars['items']);
}
/**
* Helper function to recursively alter menu items.
*/
function _fett_preprocess_menu_alter_items(&$items) {
foreach ($items as &$item) {
$options = $item['url']->getOptions();
$options['attributes']['class'][] = 'menu-item';
$item['url']->setOptions($options);
$item['link_attributes'] = new Attribute($options['attributes']);
if (!empty($item['below'])) {
$item['below'] = _fett_preprocess_menu_alter_items($item['below']);
}
}
return $items;
}
function fett_preprocess_menu_local_task(&$variables) {
$url = $variables['link']['#url'];
$state = !empty($variables['element']['#active']) ? 'active' : 'secondary';
$options = $url->getOptions();
$options['attributes']['class'][] = 'button';
$options['attributes']['class'][] = 'admin';
$options['attributes']['class'][] = $state;
$url->setOptions($options);
}