-
Notifications
You must be signed in to change notification settings - Fork 14
/
oe_bootstrap_theme.theme
130 lines (106 loc) · 4.13 KB
/
oe_bootstrap_theme.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
<?php
/**
* @file
* The oe_bootstrap_theme hooks.
*/
declare(strict_types=1);
use Drupal\Core\Template\Attribute;
// Include all files from the includes directory.
$includes_path = __DIR__ . '/includes/*.inc';
foreach (glob($includes_path) as $filename) {
// The inspection disallowing basename() is not relevant for known paths.
// phpcs:ignore QualityAssurance.Functions.DrupalWrappers.FoundWithAlternative
require_once __DIR__ . '/includes/' . basename($filename);
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for table.
*/
function oe_bootstrap_theme_theme_suggestions_table_alter(array &$suggestions, array $variables) {
if (!theme_get_setting('bootstrap_tables.enable')) {
return;
}
$extra_suggestions[] = 'table__bootstrap';
$responsive = theme_get_setting('bootstrap_tables.responsive');
if (is_string($responsive) && $responsive !== '') {
$extra_suggestions[] = 'table__bootstrap__responsive';
}
// Add the new suggestions first in the list, so existing child theme
// overrides will keep precedence.
array_unshift($suggestions, ...$extra_suggestions);
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for views-view-table.
*/
function oe_bootstrap_theme_theme_suggestions_views_view_table_alter(array &$suggestions, array $variables) {
if (!theme_get_setting('bootstrap_tables.enable')) {
return;
}
$extra_suggestions[] = 'views_view_table__bootstrap';
$responsive = theme_get_setting('bootstrap_tables.responsive');
if (is_string($responsive) && $responsive !== '') {
$extra_suggestions[] = 'views_view_table__bootstrap__responsive';
}
// Add the new suggestions first in the list, so existing child theme
// overrides will keep precedence.
array_unshift($suggestions, ...$extra_suggestions);
}
/**
* Implements hook_preprocess().
*/
function oe_bootstrap_theme_preprocess(&$variables) {
// Get the active theme.
$theme = \Drupal::theme()->getActiveTheme();
// Get the theme handler service.
$theme_handler = \Drupal::service('theme_handler');
// Initialize variables for the theme path and icon path.
$bcl_icon_path = NULL;
$theme_path = '';
// Traverse up the theme chain until the root theme (oe_bootstrap_theme).
while ($theme) {
$theme_name = $theme->getName();
$theme_info = $theme_handler->getTheme($theme_name)->info;
// Check if the BCL icon path is set in the current theme.
if (!empty($theme_info['openeuropa']['bootstrap_component_library']['icon_path'])) {
$bcl_icon_path = $theme_info['openeuropa']['bootstrap_component_library']['icon_path'];
$theme_path = $theme_handler->getTheme($theme_name)->getPath();
break;
}
// Move to the base theme (if any).
$parent_theme = $theme_info['base theme'] ?? NULL;
$theme = $parent_theme ? $theme_handler->getTheme($parent_theme) : NULL;
}
// If no BCL icon path was found, use the default path.
if (!$bcl_icon_path) {
$oe_bootstrap_theme = $theme_handler->getTheme('oe_bootstrap_theme');
$bcl_icon_path = 'assets/icons/bcl-default-icons.svg';
$theme_path = $oe_bootstrap_theme->getPath();
}
// Construct the full URL for the BCL icons.
$variables['bcl_icon_path'] = base_path() . $theme_path . '/' . $bcl_icon_path;
}
/**
* Implements hook_preprocess_HOOK() for table--bootstrap--responsive.html.twig.
*/
function oe_bootstrap_theme_preprocess_table__bootstrap__responsive(&$variables) {
_oe_bootstrap_theme_bootstrap_responsive_table($variables);
}
/**
* Implements hook_preprocess_HOOK() for views-view-table--bootstrap--responsive.html.twig.
*/
function oe_bootstrap_theme_preprocess_views_view_table__bootstrap__responsive(&$variables) {
_oe_bootstrap_theme_bootstrap_responsive_table($variables);
}
/**
* Function that applies responsiveness to Bootstrap tables.
*
* @param array $variables
* The variables to process.
*/
function _oe_bootstrap_theme_bootstrap_responsive_table(array &$variables): void {
$class = 'table-responsive';
$breakpoint = theme_get_setting('bootstrap_tables.responsive');
if ($breakpoint !== 'always') {
$class .= '-' . $breakpoint;
}
$variables['wrapper_attributes'] = new Attribute(['class' => $class]);
}