-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulma.theme
453 lines (369 loc) · 11.5 KB
/
bulma.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
/**
* @file
* The primary PHP file for the Drupal Bulma base theme.
*/
use Drupal\block\Entity\Block;
use Drupal\bulma\Bulma;
use Drupal\bulma\Bulmaswatch;
use Drupal\file\Entity\File;
/**
* Implements template_preprocess_page().
*/
function bulma_preprocess_page(&$variables) {
// Site slogan as separate variable.
$site_config = \Drupal::config('system.site');
$variables['site_slogan'] = $site_config->get('slogan');
}
/**
* Implements template_preprocess_file_link().
*/
function bulma_preprocess_file_link(&$variables) {
/** @var \Drupal\Core\Entity\Entity\File $file */
$file = ($variables['file'] instanceof File) ? $variables['file'] : File::load($variables['file']->fid);
$variables['icon'] = Bulma::getFileIcon($file);
}
/**
* Implements hook_page_attachments_alter().
*/
function bulma_page_attachments_alter(&$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 bulma_preprocess_HOOK() for breadcrumb.
*/
function bulma_preprocess_breadcrumb(&$variables) {
// Load Bulma class from settings.
$classes = array_merge(['breadcrumb'], Bulma::multiSettings('breadcrumb'));
foreach ($classes as $class) {
$variables['attributes']['class'][] = $class;
}
$variables['attributes']['role'] = 'navigation';
$variables['attributes']['aria-labelledby'] = 'system-breadcrumb';
}
/**
* Implements theme_menu_local_tasks().
*/
function bulma_preprocess_menu_local_tasks(&$variables) {
// Load Bulma class from settings.
$bulma_tabs_class = Bulma::multiSettings('tabs');
if (!empty($bulma_tabs_class)) {
// Set variables.
foreach ($bulma_tabs_class as $key => $class) {
$variables[str_replace('is-', '', $key)] = $class;
}
}
}
/**
* Implements hook_form_alter().
*/
function bulma_form_alter(&$form, $form_state, $form_id) {
// Check for actions buttons, group them.
if (isset($form['actions'])) {
$form['actions']['#attributes']['class'][] = 'field';
$form['actions']['#attributes']['class'][] = 'is-grouped';
// Loop trough array to get link type.
// Better approach maybe to use https://www.drupal.org/project/button_link.
foreach ($form['actions'] as $key => $action) {
if (isset($action['#type'])) {
if ($action['#type'] == 'link') {
$name = (string) $action['#title'];
// Check if button should be colorized.
$button_color = Bulma::colorizeButton($name);
// Add button class.
if (!empty($button_color)) {
foreach ($button_color as $class) {
$form['actions'][$key]['#attributes']['class'][] = $class;
}
}
}
}
}
}
switch ($form_id) {
case 'node_preview_form_select':
$form['backlink']['#options']['attributes']['class'][] = 'button';
break;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function bulma_form_search_form_alter(&$form, $form_state, $form_id) {
// Remove label.
$form['basic']['keys']['#title_display'] = 'invisible';
// Update the placeholder.
$form['basic']['keys']['#placeholder'] = t('Enter your keywords');
// Wrap class to combine form.
$form['basic']['#attributes']['class'] = 'field has-addons';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function bulma_form_search_block_form_alter(&$form, $form_state, $form_id) {
// Update the placeholder, remove title - prevent double icon.
$form['keys']['#title'] = '';
$form['keys']['#placeholder'] = t('Search');
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function bulma_theme_suggestions_form_alter(array &$suggestions, array $variables) {
if ($variables['element']['#form_id'] === 'search_block_form') {
$suggestions[] = 'form__search_block_form';
}
}
/**
* Implements template_preprocess_block().
*/
function bulma_preprocess_block(&$variables) {
// Block as panel style.
if (Bulma::singleSetting('general.block')) {
if ($variables['configuration']['label_display']) {
$variables['block_panel_style'] = TRUE;
}
}
// Customize the ID and classes of the main menu block.
$main_menu = Bulma::singleSetting('general.menu');
$plugin_id = 'system_menu_block:' . $main_menu;
// Match blocks by plugin ID to support Bulma and any active subtheme.
if (!empty($variables['elements']['#plugin_id']) && $variables['elements']['#plugin_id'] == $plugin_id) {
$variables['attributes']['id'] = 'navbar-menu';
$variables['attributes']['class'][] = 'navbar-end';
}
// Set a region variable for use in templates.
if (isset($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if ($block) {
$variables['region'] = $block->getRegion();
}
}
}
/**
* Implements template_preprocess_table().
*/
function bulma_preprocess_table(&$variables) {
// Load existing classes.
$existing_class = [];
if (isset($variables['attributes']['class'])) {
$existing_class = $variables['attributes']['class'];
}
// Load Bulma specific table classes from theme settings.
$bulma_table_class = Bulma::multiSettings('table');
// Merge classes.
if (!empty($bulma_table_class)) {
$variables['attributes']['class'] = array_merge($existing_class, $bulma_table_class);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function bulma_theme_suggestions_links_alter(array &$suggestions, array $variables) {
$link_type = $variables['theme_hook_original'];
// We want only affect node and comment links.
$target_output = ['links__node', 'links__comment'];
if (in_array($link_type, $target_output)) {
$suggestions[] = 'links__bulma';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function bulma_theme_suggestions_input_alter(array &$suggestions, array $variables) {
// Define defaults for exclusion for rendering as bulma input.
$exclude_input = [
'hidden',
'token',
'radio',
'checkbox',
'submit',
'image_button',
];
// Submit button needs have render as button element.
if ($variables['element']['#type'] == 'submit') {
$suggestions[] = 'input__button__bulma';
}
// Add theme suggestions for others input elements.
if (!in_array($variables['element']['#type'], $exclude_input)) {
// Default wrapped input.
$suggestions[] = 'input__bulma';
}
}
/**
* Implements template_preprocess_hook().
*/
function bulma_preprocess_input__bulma(&$variables) {
// Make sure we have data to work from.
if (empty($variables['element']['#type']) || empty($variables['element']['#title'])) {
return;
}
// Field name and type.
$type = $variables['element']['#type'];
$name = (string) $variables['element']['#title'];
// Check if we use icons.
if (Bulma::singleSetting('general.icon')) {
$variables['icon'] = Bulma::iconMatch($name, $type);
}
// Size of input buttons.
$input_size = Bulma::singleSetting('bulma_elements_input_size');
if ($input_size != 'none') {
$variables['attributes']['class'][] = $input_size;
}
}
/**
* Implements template_preprocess_hook().
*/
function bulma_preprocess_input__button__bulma(&$variables) {
// Field name and type.
$name = $variables['element']['#value'];
$type = $variables['element']['#type'];
// We need render label inside button tag.
$variables['label'] = $variables['element']['#value'];
// Bulma submit classes.
$variables['bulma_button_classes'] = [];
// Check if button should be colorized.
$button_color = Bulma::colorizeButton($name);
if (!empty($button_color)) {
$variables['bulma_button_classes'] = $button_color;
}
// Check if we use icons.
if (Bulma::singleSetting('general.icon')) {
// Define default if not matched.
$variables['icon'] = Bulma::iconMatch($name, $type);
// Hide label for search button.
if ($variables['icon'] == 'search') {
$variables['label'] = FALSE;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function bulma_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
// Navbar menu.
$bulma_nav = Bulma::singleSetting('general.menu');
// Check if any menu is selected to be nav menu.
if ($bulma_nav != 'none') {
if ($variables['menu_name'] === $bulma_nav) {
$suggestions[] = 'menu__nav_bulma';
}
}
}
/**
* Implements template_preprocess_form_element().
*/
function bulma_preprocess_form_element(&$variables) {
// Render the form element inside the label for checkbox and radio.
if (in_array($variables['element']['#type'], ['checkbox', 'radio'])) {
$variables['label']['#children'] = $variables['children'];
unset($variables['children']);
}
}
/**
* Implements template_preprocess_form_element_label().
*/
function bulma_preprocess_form_element_label(&$variables) {
// Check additional theme labels settings.
$bulma_labels = Bulma::multiSettings('label');
if (!empty($bulma_labels)) {
foreach ($bulma_labels as $key => $value) {
$variables[$key] = $value;
}
}
}
/**
* Implements template_preprocess_page_title().
*/
function bulma_preprocess_page_title(&$variables) {
$variables['title_attributes']['class'] = 'title';
}
/**
* Implements template_preprocess_views_view_table().
*/
function bulma_preprocess_views_view_table(&$variables) {
// Define defaults for variables.
$variables['bordered'] = FALSE;
$variables['striped'] = FALSE;
$variables['narrow'] = FALSE;
$variables['table'] = TRUE;
// Load Bulma specific table classes from theme settings.
$bulma_table_class = Bulma::multiSettings('table');
if (!empty($bulma_table_class)) {
foreach ($bulma_table_class as $class) {
$variables[str_replace('is-', '', $class)] = TRUE;
}
}
}
/**
* Implements hook_library_info_alter().
*/
function bulma_library_info_alter(&$libraries, $extension) {
// Swap in Bulmaswatch theme's CSS.
if ($extension === 'bulma' &&
isset($libraries['global']) &&
($theme = theme_get_setting('cdn.bulmaswatch.theme'))
) {
// If bulmaswatch is configured to use the default theme, use Bulma directly.
$cdn_data = Bulma::getCdnData();
$properties = [];
if ($theme === 'default') {
$bulma_file = '/css/bulma.css';
if (Bulma::isLocal()) {
$css_file = '/libraries/bulma' . $bulma_file;
}
else {
$css_file = $cdn_data['api']['bulma']['file_root'] . $bulma_file;
$properties['type'] = 'external';
}
}
else {
$properties['minified'] = TRUE;
$themes = Bulmaswatch::getThemes();
if (isset($themes[$theme]['local_css'])) {
$css_file = $themes[$theme]['local_css'];
}
else {
$css_file = $cdn_data['api']['bulmaswatch']['file_root'] . "/{$theme}/bulmaswatch.min.css";
$properties['type'] = 'external';
}
}
$libraries['global']['css']['base'] = [
$css_file => $properties,
];
}
}