-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsulib_base.theme
More file actions
347 lines (302 loc) · 10.9 KB
/
psulib_base.theme
File metadata and controls
347 lines (302 loc) · 10.9 KB
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
<?php
/**
* @file
* PSU Libraries Base theme file.
*/
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\node\NodeInterface;
/**
* Implements hook_preprocess_page().
*/
function psulib_base_preprocess_page(array &$page) {
// Create a variable to show column layout.
$sidebar_first_content = isset($page['page']['sidebar_first']) ? \Drupal::service('renderer')->render($page['page']['sidebar_first']) : '';
$sidebar_first_real_content = preg_replace('/<!--(.*)-->/Uis', '', trim($sidebar_first_content));
$page['has_sidebar_first'] = (bool) strlen(trim($sidebar_first_real_content));
$sidebar_second_content = isset($page['page']['sidebar_second']) ? \Drupal::service('renderer')->render($page['page']['sidebar_second']) : '';
$sidebar_second_real_content = preg_replace('/<!--(.*)-->/Uis', '', trim($sidebar_second_content));
$page['has_sidebar_second'] = (bool) strlen(trim($sidebar_second_real_content));
// Add logo and site for footer.
$page['site_logo'] = base_path() . \Drupal::theme()->getActiveTheme()->getLogo();
}
/**
* Implements hook_page_attachments_alter().
*
* Preloading webfonts that will be used on every page to limit FOUT.
*/
function psulib_base_page_attachments_alter(array &$attachments) {
$theme_path = \Drupal::service('extension.list.theme')->getPath('psulib_base');
$files = [
// Roboto is the default font.
base_path() . $theme_path . '/assets/fonts/Roboto/roboto-regular-webfont.woff2',
base_path() . $theme_path . '/assets/fonts/Roboto/roboto-bold-webfont.woff2',
base_path() . $theme_path . '/assets/fonts/Roboto/roboto-italic-webfont.woff2',
// Roboto Slab is used for <h1> elements.
base_path() . $theme_path . '/assets/fonts/Roboto_Slab/static/RobotoSlab-Bold.woff2',
// Roboto Condensed used for navigation elements (menus and breadcrumbs).
base_path() . $theme_path . '/assets/fonts/Roboto_Condensed/RobotoCondensed-VariableFont_wght.woff2',
];
foreach ($files as $path) {
$file_parts = explode('.', basename($path));
$extension = end($file_parts);
$attachments['#attached']['html_head_link'][] = [
[
'rel' => 'preload',
'href' => $path,
'as' => 'font',
'type' => 'font/' . $extension,
'crossorigin' => 'crossorigin',
],
TRUE,
];
}
}
/**
* Implements hook_element_info_alter().
*/
function psulib_base_element_info_alter(array &$types) {
if (isset($types['layout_builder'])) {
$types['layout_builder']['#attached']['library'][] = 'psulib_base/layout_builder';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function psulib_base_preprocess_layout(&$variables) {
// Check if the layout section is actually empty.
$route_name = \Drupal::routeMatch()->getRouteName();
$is_layout_builder_edit = strpos($route_name, 'layout_builder') !== FALSE;
$is_empty = TRUE;
if (!$is_layout_builder_edit) {
foreach (Element::children($variables['content']) as $key) {
// Check if this child element has any content.
$child_content = \Drupal::service('renderer')->render($variables['content'][$key]);
$child_real_content = preg_replace('/<!--(.*)-->/Uis', '', trim($child_content));
$has_content = (bool) strlen(trim($child_real_content));
if ($has_content) {
$is_empty = FALSE;
break;
}
}
if ($is_empty) {
$variables['content_is_empty'] = TRUE;
}
}
// Set an id so we can link to directly to this layout section.
if (empty($variables['settings']['label'])) {
return;
}
$class = [
Html::getClass('layout-section-' . $variables['settings']['label']),
];
if (!isset($variables['attributes']['class']) || !is_array($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
$variables['attributes']['class'] = array_merge($variables['attributes']['class'], $class);
$id = Html::getUniqueId('layout-section-' . $variables['settings']['label']);
$variables['attributes']['id'] = $id;
}
/**
* Implements hook_preprocess_HOOK() for menu-local-tasks templates.
*
* Use preprocess hook to set #attached to child elements
* because they will be processed by Twig and drupal_render will
* be invoked.
*/
function psulib_base_preprocess_menu_local_tasks(&$variables) {
if (!empty($variables['primary'])) {
$variables['primary']['#attached'] = [
'library' => [
'psulib_base/drupal.nav-tabs',
],
];
}
elseif (!empty($variables['secondary'])) {
$variables['secondary']['#attached'] = [
'library' => [
'psulib_base/drupal.nav-tabs',
],
];
}
foreach (Element::children($variables['primary']) as $key) {
$variables['primary'][$key]['#level'] = 'primary';
}
foreach (Element::children($variables['secondary']) as $key) {
$variables['secondary'][$key]['#level'] = 'secondary';
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-task templates.
*/
function psulib_base_preprocess_menu_local_task(&$variables) {
// Ensure is-active class is set when the tab is active. The generic active
// link handler applies stricter comparison rules than what is necessary for
// tabs.
if (isset($variables['is_active']) && $variables['is_active'] === TRUE) {
$variables['link']['#options']['attributes']['class'][] = 'nav-link active is-active';
}
else {
$variables['link']['#options']['attributes']['class'][] = 'nav-link';
}
if (isset($variables['element']['#level'])) {
$variables['level'] = $variables['element']['#level'];
}
}
/**
* Callback to convert attributes class which is string to array.
*
* @param array $variables
* The variables to render from preprocess function.
*/
function _psulib_base_attributes_class_string_to_array(&$variables) {
if (isset($variables['attributes']) &&
isset($variables['attributes']['class']) &&
!is_array($variables['attributes']['class'])) {
$variables['attributes']['class'] = [$variables['attributes']['class']];
}
}
/**
* Implements theme_preprocess_textarea().
*/
function psulib_base_preprocess_textarea(&$variables) {
_psulib_base_attributes_class_string_to_array($variables);
$variables['attributes']['class'][] = 'form-control';
}
/**
* Implements template_preprocess_HOOK() for input.
*/
function psulib_base_preprocess_input(&$variables) {
_psulib_base_attributes_class_string_to_array($variables);
$type_html = $variables['attributes']['type'];
$text_types_html = [
'text',
'email',
'tel',
'number',
'search',
'password',
'url',
'month',
'week',
'date',
'time',
];
if (in_array($type_html, $text_types_html)) {
$variables['attributes']['class'][] = 'form-control';
}
if (in_array($type_html, ['checkbox'])) {
$variables['attributes']['class'][] = 'form-check-input';
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Pass block region value to content so this can be used in
* THEME_theme_suggestions_menu_alter.
*/
function psulib_base_preprocess_block(&$variables) {
if (isset($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if ($block) {
$region = Block::load($variables['elements']['#id'])->getRegion();
if (!empty($region)) {
$variables['content']['#attributes']['data-block']['region'] = $region;
}
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for page.
*
* Add layout builder suggestions for pages that use Layout Builder.
*/
function psulib_base_theme_suggestions_page_alter(array &$suggestions, array $variables) {
$node = \Drupal::routeMatch()->getParameter('node');
if (isset($node) && $node instanceof NodeInterface) {
if ($node->hasField('layout_builder__layout') && $node->get('layout_builder__layout')->getValue()) {
$suggestions[] = 'page__node__lb_display';
$suggestions[] = 'page__node__' . $node->bundle() . '__lb_display';
}
$suggestions[] = 'page__node__' . $node->bundle();
}
}
/**
* Implements hook_preprocess_media_library_item__widget().
*
* This targets each media item selected in an entity reference field.
*/
function psulib_base_preprocess_media_library_item__widget(array &$variables) {
$variables['content']['remove_button']['#attributes']['class'][] = 'media-library-item__remove';
$variables['content']['remove_button']['#attributes']['class'][] = 'icon-link';
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for menu.
*/
function psulib_base_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
if (isset($variables['attributes']['data-block']['region'])) {
$region = $variables['attributes']['data-block']['region'];
$suggestions[] = $variables['theme_hook_original'] . '__' . $region;
$suggestions[] = 'menu__' . $region;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for field.
*
* Adding additional field suggestions.
*/
function psulib_base_theme_suggestions_field_alter(array &$suggestions, array $variables) {
$suggestions[] = 'field__' . $variables['element']['#entity_type'] . '__' . $variables['element']['#field_name'] . '__' . $variables['element']['#bundle'] . '__view_mode_' . $variables['element']['#view_mode'];
$suggestions[] = 'field__' . $variables['element']['#entity_type'] . '__' . $variables['element']['#field_name'] . '__view_mode_' . $variables['element']['#view_mode'];
}
/**
* Implements hook_plugin_filter_TYPE__CONSUMER_alter() for layout_builder.
*
* Remove default layouts from Layout Builder.
*/
function psulib_base_plugin_filter_layout__layout_builder_alter(array &$variables, array $extra) {
unset($variables['layout_onecol']);
unset($variables['layout_twocol_section']);
unset($variables['layout_threecol_section']);
unset($variables['layout_fourcol_section']);
}
/**
* Implements theme_preprocess_image().
*/
function psulib_base_preprocess_image(&$variables) {
_psulib_base_attributes_class_string_to_array($variables);
$variables['attributes']['class'][] = 'img-fluid';
}
/**
* Implements theme_preprocess_table().
*/
function psulib_base_preprocess_table(&$variables) {
_psulib_base_attributes_class_string_to_array($variables);
$variables['attributes']['class'][] = 'table';
$variables['attributes']['class'][] = 'table-striped';
}
/**
* Implements theme_preprocess_breadcrumb().
*/
function psulib_base_preprocess_breadcrumb(&$variables) {
if ($variables['breadcrumb']) {
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
if (!empty($page_title)) {
$variables['breadcrumb'][] = [
'text' => $page_title,
'attributes' => new Attribute(['class' => ['active']]),
];
}
$variables['#cache']['contexts'][] = 'url';
}
}
/**
* Implements hook_preprocess_HOOK() for form elements.
*/
function psulib_base_preprocess_webform(&$variables) {
$variables['#attached']['library'][] = 'psulib_base/webform-validation';
}