-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui_suite_bootstrap.theme
177 lines (165 loc) · 5.76 KB
/
ui_suite_bootstrap.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
<?php
/**
* @file
* Functions to support theming in the ui_suite_bootstrap theme.
*/
declare(strict_types = 1);
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\AttributeHelper;
/**
* Add a value into the class attributes of a given element.
*
* Assumes element is an array.
* Inspired by: https://www.drupal.org/node/3334294
*
* @param array $element
* An array.
* @param string[]|string ...$classes
* The classes to add on element. Arguments can include string keys directly,
* or arrays of string keys.
*
* @return array
* The element with the given class(es) in attributes.
*/
function _ui_suite_bootstrap_add_class(array $element, ...$classes): array {
if (array_is_list($element)) {
foreach ($element as $index => $item) {
if (!\is_array($item)) {
continue;
}
$element[$index] = _ui_suite_bootstrap_add_class($item, ...$classes);
}
return $element;
}
$attributes = new Attribute($element['#attributes'] ?? []);
$attributes->addClass(...$classes);
$element['#attributes'] = $attributes->toArray();
return $element;
}
/**
* Set attribute on a given element.
*
* Assumes element is an array.
* Inspired by: https://www.drupal.org/node/3334294
*
* @param array $element
* An array.
* @param string $name
* The attribute name.
* @param mixed $value
* (optional) The attribute value.
*
* @return array
* The element with the given sanitized attribute's value.
*/
function _ui_suite_bootstrap_set_attribute(array $element, string $name, string $value): array {
if (array_is_list($element)) {
foreach ($element as $index => $item) {
if (!\is_array($item)) {
continue;
}
$element[$index] = _ui_suite_bootstrap_set_attribute($item, $name, $value);
}
return $element;
}
$element['#attributes'] = AttributeHelper::mergeCollections(
$element['#attributes'] ?? [],
new Attribute([$name => $value])
);
return $element;
}
/**
* Implements hook_preprocess_HOOK() for 'page'.
*/
function ui_suite_bootstrap_preprocess_page(array &$variables): void {
// Will be replaced by UI Skins.
$variables['container'] = 'container';
}
/**
* Implements hook_preprocess_HOOK() for 'pattern_card'.
*
* See also: https://getbootstrap.com/docs/4.6/components/card/.
*/
function ui_suite_bootstrap_preprocess_pattern_card(array &$variables): void {
if (array_key_exists('image', $variables) && is_array($variables['image'])) {
$class = 'card-img-top';
if ($variables['variant'] === 'overlay') {
$class = 'card-img';
}
$variables['image'] = _ui_suite_bootstrap_add_class($variables['image'], $class);
}
if (array_key_exists('links', $variables) && is_array($variables['links'])) {
$variables['links'] = _ui_suite_bootstrap_add_class($variables['links'], 'card-link');
}
}
/**
* Implements hook_preprocess_HOOK() for 'pattern_carousel'.
*
* See also: https://getbootstrap.com/docs/4.6/components/carousel/.
*/
function ui_suite_bootstrap_preprocess_pattern_carousel(array &$variables): void {
// Nicer preview with fixed width.
if ($variables['context']->getType() == 'preview') {
$variables['attributes']['style'] = "width: 800px";
}
}
/**
* Implements hook_preprocess_HOOK() for 'pattern_figure'.
*
* See also: https://getbootstrap.com/docs/4.6/components/figures/.
*/
function ui_suite_bootstrap_preprocess_pattern_figure(array &$variables): void {
if (array_key_exists('image', $variables) && is_array($variables['image'])) {
$variables['image'] = _ui_suite_bootstrap_add_class($variables['image'], 'figure-img');
}
}
/**
* Implements hook_preprocess_HOOK() for 'pattern_pagination'.
*
* See also: https://getbootstrap.com/docs/4.6/components/pagination/.
*/
function ui_suite_bootstrap_preprocess_pattern_pagination(array &$variables): void {
$variables['current'] = (int) (string) $variables['current'];
}
/**
* Implements hook_preprocess_HOOK() for 'pattern_drowpdown'.
*
* See also: https://getbootstrap.com/docs/4.6/components/dropdowns/.
*/
function ui_suite_bootstrap_preprocess_pattern_dropdown(array &$variables): void {
if (isset($variables['split']) && $variables['split']) {
if (array_is_list($variables['button'])) {
$variables['button'] = $variables['button'][0];
}
$split_button = $variables['button'];
unset($split_button['#label']);
unset($split_button['#url']);
$split_button = _ui_suite_bootstrap_set_attribute($split_button, 'data-toggle', 'dropdown');
$split_button = _ui_suite_bootstrap_set_attribute($split_button, 'data-toggle', 'dropdown-toggle-split');
$split_button = _ui_suite_bootstrap_set_attribute($split_button, 'aria-expanded', 'false');
$split_button = _ui_suite_bootstrap_add_class($split_button, 'dropdown-toggle');
$split_button = _ui_suite_bootstrap_add_class($split_button, 'dropdown-toggle-split');
$variables['button'] = [
$variables['button'],
$split_button,
];
return;
}
if (array_key_exists('button', $variables) && is_array($variables['button'])) {
$variables['button'] = _ui_suite_bootstrap_set_attribute($variables['button'], 'data-toggle', 'dropdown');
$variables['button'] = _ui_suite_bootstrap_set_attribute($variables['button'], 'aria-expanded', 'false');
$variables['button'] = _ui_suite_bootstrap_add_class($variables['button'], 'dropdown-toggle');
}
}
/**
* Implements hook_preprocess_HOOK() for 'image'.
*
* Convert relative path to full path for card, carousel & figure previews.
*/
function ui_suite_bootstrap_preprocess_image(array &$variables): void {
$uri = $variables["uri"];
$is_absolute = isset(parse_url($uri)['host']) || \str_starts_with($uri, "/") || \str_starts_with($uri, "data:");
if (!$is_absolute) {
$variables["attributes"]["src"] = base_path() . $variables["directory"] . "/" . $uri;
}
}