-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxpr_theme.theme
More file actions
621 lines (551 loc) · 20.4 KB
/
dxpr_theme.theme
File metadata and controls
621 lines (551 loc) · 20.4 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Template\Attribute;
use Drupal\block\Entity\Block;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_library_info_alter().
*
* Replaces style sheets declared in libraries with color-altered style sheets.
*/
function dxpr_theme_library_info_alter(&$libraries, $extension) {
if ($extension == 'dxpr_theme') {
// Manage dxpr_theme themesettings CSS.
$current_theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
$key = 'public://dxpr_theme/css/themesettings-' . $current_theme . '.css';
if (\Drupal::service('file_system')->realpath($key)) {
$libraries['global-styling']['css']['theme'][$key] = [
'minified' => FALSE,
'weight' => 5,
];
}
// Subthemes dxpr_theme.css.
if ($current_theme !== 'dxpr_theme') {
// Subthemes carry their own copy of dxpr_theme.css,
// disable dxpr_theme/css/dxpr_theme.css.
unset($libraries['global-styling']['css']['theme']['css/dxpr_theme.css']);
}
}
}
/**
* Prepares variables for the all templates. Theme settings.
*/
function dxpr_theme_preprocess(&$variables, $hook, $info) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$variables['theme']['settings'] = \Drupal::config($theme . '.settings')->get();
}
/**
* Prepares variables for the html template. Adds node object.
*
* Default template: html.html.twig.
*
* See the html.html.twig template for the list of variables.
*/
function dxpr_theme_preprocess_html(&$variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$variables['node'] = $node;
}
foreach (\Drupal::routeMatch()->getParameters() as $parameter) {
if ($parameter instanceof ContentEntityInterface) {
$variables['attributes']['class'][] = Html::getClass('page-' . $parameter->getEntityTypeId() . '-' . $parameter->id());
break;
}
}
// Include CSS variables and set color scheme class.
$variables['#attached']['library'][] = 'dxpr_theme/variables';
$variables['html_attributes']
->setAttribute('class', 'dxpr-scheme-' . (theme_get_setting('color_scheme') ?? 'default'));
// Body background image.
if (array_key_exists('node', $variables)) {
$node = $variables['node'];
if (isset($node) && !is_string($node)) {
if ($node->hasField('field_dth_body_background')) {
// Get the module handler service.
$module_handler = \Drupal::service('module_handler');
// Check if the module is enabled.
if ($module_handler->moduleExists('media_library')) {
$media = $node->get('field_dth_body_background')->entity ?? 0;
if ($media instanceof MediaInterface) {
$thumbnailUri = $media->get('thumbnail')->entity->getFileUri();
$url = \Drupal::service('file_url_generator')->generateString($thumbnailUri);
if (isset($url)) {
$variables['body_bg_image_path'] = $url;
}
}
}
}
}
}
// Load Sooper Features Controllers.
$dxprThemePath = \Drupal::service('extension.list.theme')->getPath('dxpr_theme');
foreach (\Drupal::service('file_system')->scanDirectory($dxprThemePath . '/features', '/controller.inc/i') as $file) {
require_once $file->uri;
$function_name = basename($file->filename, '.inc');
$function_name = str_replace('-', '_', $function_name);
if (function_exists($function_name)) {
$function_name($variables);
}
}
if ($custom_js = theme_get_setting('custom_javascript_site')) {
$custom_js = Markup::create($custom_js);
$variables['page_bottom']['custom_javascript_site'] = [
'#markup' => $custom_js,
];
}
$variables['page_bottom']['custom_javascript'] = [
'#markup' => '<div class="hidden" id="dxpr-theme-js-seed">sfy39587stp18</div>',
];
if ($header_height = theme_get_setting('header_top_height')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerHeight'] = $header_height;
}
if ($header_scroll_offset = theme_get_setting('header_top_height_sticky_offset')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerOffset'] = $header_scroll_offset;
}
// Mobile header breakpoint.
if ($header_mobile_breakpoint = theme_get_setting('header_mobile_breakpoint')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerMobileBreakpoint'] = $header_mobile_breakpoint;
}
// Fixed mobile header.
if ($header_mobile_fixed = theme_get_setting('header_mobile_fixed')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerMobileFixed'] = $header_mobile_fixed;
}
// Mobile header height.
if ($header_mobile_height = theme_get_setting('header_mobile_height')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerMobileHeight'] = $header_mobile_height;
}
if ($header_menu_direction = ((theme_get_setting('header_side_direction') !== NULL)) ? theme_get_setting('header_side_direction') : 'left') {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['headerSideDirection'] = $header_menu_direction;
}
// Second header sticky.
if ($second_header_sticky = theme_get_setting('second_header_sticky')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['secondHeaderSticky'] = $second_header_sticky;
}
// Menu hamburger animation.
if ($header_hamburger_animation = theme_get_setting('hamburger_animation')) {
$variables['#attached']['drupalSettings']['dxpr_themeSettings']['hamburgerAnimation'] = $header_hamburger_animation;
}
// Add Apple Touch Icon URL if configured.
$web_icons_media_id = theme_get_setting('web_icons_upload');
if ($web_icons_media_id) {
$media = Media::load($web_icons_media_id);
if ($media && $media->field_media_image->entity) {
$image_style = ImageStyle::load('apple_touch_icon');
if ($image_style) {
$variables['apple_touch_icon_url'] = $image_style->buildUrl($media->field_media_image->entity->getFileUri());
}
}
}
// Add web manifest URL if icons are configured.
if ($web_icons_media_id) {
// Create a simple manifest JSON file in the public files directory.
$file_system = \Drupal::service('file_system');
$public_path = $file_system->realpath('public://');
$manifest_path = $public_path . '/manifest.webmanifest';
_dxpr_theme_generate_manifest($web_icons_media_id, $manifest_path);
// Absolute URL for the manifest.
$variables['manifest_url'] = \Drupal::service('file_url_generator')
->generateAbsoluteString('public://manifest.webmanifest');
}
}
/**
* Implements template_preprocess_page()
*/
function dxpr_theme_preprocess_page(&$variables) {
$variables['navbar_attributes'] = [
'class' => ['navbar'],
'id' => ['navbar'],
'role' => ['banner'],
];
$variables['navbar_attributes'] = new Attribute($variables['navbar_attributes']);
$hide_regions = $local_hide = [];
if (theme_get_setting('hidden_regions') && !empty(array_keys(array_filter(theme_get_setting('hidden_regions'))))) {
$hide_regions = array_keys(array_filter(theme_get_setting('hidden_regions')));
}
if (!empty($variables['node'])
&& $variables['node'] instanceof NodeInterface
&& $variables['node']->hasField('field_dth_hide_regions')) {
$local_hide = array_column($variables['node']->get('field_dth_hide_regions')->getValue(), 'value');
$hide_regions = array_unique(array_merge($hide_regions, $local_hide));
}
if (in_array('navigation', $hide_regions)) {
$hide_regions[] = 'navigation_collapsible';
}
if (in_array('header', $hide_regions)) {
$hide_regions[] = 'navigation_collapsible';
$hide_regions[] = 'navigation';
}
foreach ($hide_regions as $value) {
if (!empty($variables['page'][$value])) {
unset($variables['page'][$value]);
}
}
// Title background image.
if (array_key_exists('node', $variables)) {
$node = $variables['node'];
if (isset($node) && !is_string($node)) {
if ($node->hasField('field_dth_page_title_backgrou')) {
// Get the module handler service.
$module_handler = \Drupal::service('module_handler');
// Check if the module is enabled.
if ($module_handler->moduleExists('media_library')) {
$media = $node->get('field_dth_page_title_backgrou')->entity ?? 0;
if ($media instanceof MediaInterface) {
$thumbnailUri = $media->get('thumbnail')->entity->getFileUri();
$url = \Drupal::service('file_url_generator')->generateString($thumbnailUri);
if (isset($url)) {
$variables['title_bg_image_path'] = $url;
}
}
}
}
}
}
// CSS to load on every page.
$dxpr_theme_libraries = [
// Bootstrap TAILORING.
'dxpr_theme/bootstrap-5',
'dxpr_theme/bootstrap-theme',
// @todo Include only if needed.
// DXPR THEME BASE.
'dxpr_theme/forms',
'dxpr_theme/layout',
'dxpr_theme/page-title',
'dxpr_theme/typography',
// DXPR THEME Components.
// @todo Include only if needed.
'dxpr_theme/enhanced-dropdowns',
'dxpr_theme/admin',
// Overrides.
// @todo Include only if needed.
'dxpr_theme/drupal-webform',
'dxpr_theme/dxpr-theme-builder',
// HELPERS.
'dxpr_theme/helper-classes',
];
foreach ($dxpr_theme_libraries as $dxpr_theme_library) {
$variables['#attached']['library'][] = $dxpr_theme_library;
}
}
/**
* Implements hook_preprocess_menu().
*
* Workaround for Drupal core regression where Views-generated menu links don't
* receive proper active trail detection. Compares menu item URLs against the
* current page path to set active states and propagates to parent items.
*
* @see https://www.drupal.org/project/dxpr_theme/issues/3553914
* @see https://www.drupal.org/project/drupal/issues/3359511
*
* @todo Remove when core issue #3359511 is resolved.
*/
function dxpr_theme_preprocess_menu(&$variables) {
if (empty($variables['items'])) {
return;
}
$current_path = \Drupal::service('path.current')->getPath();
$is_front = \Drupal::service('path.matcher')->isFrontPage();
dxpr_theme_menu_set_active_trail($variables['items'], $current_path, $is_front);
// Ensure menu is cached per URL to prevent wrong active states.
$variables['#cache']['contexts'][] = 'url.path';
}
/**
* Recursively sets active trail on menu items by comparing URL paths.
*
* @param array $items
* Menu items array (passed by reference).
* @param string $current_path
* Current page path (e.g., "/test-view").
* @param bool $is_front
* Whether the current page is the front page.
*
* @return bool
* TRUE if any item in this level or below is active.
*/
function dxpr_theme_menu_set_active_trail(array &$items, $current_path, $is_front) {
$has_active = FALSE;
foreach ($items as &$item) {
if (empty($item['url'])) {
continue;
}
$is_active = FALSE;
$child_active = FALSE;
try {
$item_path = $item['url'] instanceof
Url ? $item['url']->toString()
: (string) $item['url'];
if ($item_path === $current_path || ($is_front && $item_path === '/')) {
$item['in_active_trail'] = TRUE;
$item['is_active'] = TRUE;
$is_active = TRUE;
}
}
catch (\Exception $e) {
// Silently continue if URL cannot be processed.
}
if (!empty($item['below'])) {
$child_active = dxpr_theme_menu_set_active_trail($item['below'], $current_path, $is_front);
if ($child_active && empty($item['in_active_trail'])) {
$item['in_active_trail'] = TRUE;
}
}
if ($is_active || $child_active) {
$has_active = TRUE;
}
}
return $has_active;
}
/**
* Implements template_menu_local_tasks()
*/
function dxpr_theme_preprocess_menu_local_tasks(&$variables) {
if ($variables['secondary']) {
// Secondary tabs are styled with bootstrap pager CSS.
$variables['#attached']['library'][] = 'dxpr_theme/drupal-pager';
}
// Check if admin_toolbar_tools is enabled and configured to show local tasks.
$module_handler = \Drupal::service('module_handler');
if ($module_handler->moduleExists('admin_toolbar_tools')) {
$config = \Drupal::config('admin_toolbar_tools.settings');
$show_local_tasks = $config->get('show_local_tasks');
if ($show_local_tasks === TRUE) {
// Only hide tabs for users who have permission to access the toolbar.
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission('access toolbar')) {
// Hide tabs when admin_toolbar_tools is showing local tasks.
$variables['hide_tabs'] = TRUE;
}
}
}
}
/**
* Implements template_preprocess_block()
*/
function dxpr_theme_preprocess_block(&$variables) {
if (isset($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if ($block) {
$variables['region'] = $block->getRegion();
// If it exists, add the 'content_class' from the block's settings to
// the content's CSS classes.
if ($block_classes = $block->getThirdPartySettings('block_classes')) {
if (!empty($block_classes['content_class'])) {
$variables['content']['#attributes']['class'][] = $block_classes['content_class'];
}
}
}
}
// Include DXPR Theme full search block css library.
if ($variables['plugin_id'] === 'full_screen_search') {
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-full-screen-search';
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-full-screen-search-js';
}
// Include DXPR Theme book navigation css library.
if ($variables['plugin_id'] === 'book_navigation') {
$variables['#attached']['library'][] = 'dxpr_theme/drupal-book';
}
}
/**
* Implements template_preprocess_links()
*/
function dxpr_theme_preprocess_links__language_block(&$variables) {
$variables['attributes']['class'][] = 'nav';
$variables['attributes']['class'][] = 'menu';
}
/**
* Implements template_preprocess_region()
*/
function dxpr_theme_preprocess_region(&$variables) {
switch ($variables['region']) {
case 'secondary_header':
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-secondary-header';
// Fall through to also attach navigation libraries.
case 'navigation':
case 'navigation_collapsible':
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-header';
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-header--top';
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-header--mobile';
$variables['#attached']['library'][] = 'dxpr_theme/dxpr-theme-navigation-colors';
break;
case 'footer':
$variables['#attached']['library'][] = 'dxpr_theme/footer-menu';
break;
}
// Apply class to block design regions.
if ($block_design_regions = array_keys(array_filter(theme_get_setting('block_design_regions')))) {
if (in_array($variables['region'], $block_design_regions)) {
$variables['attributes']['class'][] = 'region-block-design';
}
}
}
/**
* Implements template_preprocess_breadcrumb()
*/
function dxpr_theme_preprocess_breadcrumb(&$variables) {
// Remove 'Home' from breadcrumb trail.
if (count($variables['breadcrumb'])) {
array_shift($variables['breadcrumb']);
}
if (!empty($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['#attached']['library'][] = 'dxpr_theme/drupal-breadcrumbs';
// Always load breadcrumb on settings page.
if ($route_match->getRouteName() === 'dxpr_theme_helper.settings') {
$variables['theme']['settings']['page_title_breadcrumbs'] = TRUE;
}
}
}
/**
* Implements template_preprocess_comment()
*/
function dxpr_theme_preprocess_comment(&$variables) {
if ($variables['elements']['#comment']) {
$variables['#attached']['library'][] = 'bootstrap5/indented';
$variables['#attached']['library'][] = 'dxpr_theme/drupal-comments';
}
}
/**
* Implements template_preprocess_pager()
*/
function dxpr_theme_preprocess_pager(&$variables) {
$variables['#attached']['library'][] = 'dxpr_theme/drupal-pager';
}
/**
* Implements template_preprocess_book_navigation()
*/
function dxpr_theme_preprocess_book_navigation(&$variables) {
$variables['#attached']['library'][] = 'dxpr_theme/drupal-book';
}
/**
* Implements template_preprocess_views_mini_pager()
*/
function dxpr_theme_preprocess_views_mini_pager(&$variables) {
$variables['#attached']['library'][] = 'dxpr_theme/drupal-pager';
}
/**
* Implements template_theme_suggestions_form_element_alter()
*/
function dxpr_theme_theme_suggestions_form_element_alter(array &$suggestions, array $variables, $hook) {
if (isset($variables['element']['#type'])) {
$id = str_replace("-", "_", $variables['element']['#type']);
$suggestions[] = $hook . '__' . $id;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function dxpr_theme_form_search_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['basic']['#attributes']['class'] = [
0 => 'form-group',
1 => 'input-group',
];
$form['basic']['keys']['#placeholder'] = $form['basic']['keys']['#title'];
unset($form['basic']['keys']['#title']);
$form['basic']['submit']['#icon'] = TRUE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function dxpr_theme_form_search_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['actions']['#type'] = 'submit';
$form['keys']['#placeholder'] = 'Search';
$form['actions']['#icon'] = TRUE;
unset($form['actions']['submit']);
}
/**
* Implements template_preprocess_input()
*/
function dxpr_theme_preprocess_input(&$variables) {
$element = $variables['element'];
if (isset($element['#icon'])) {
$variables['icon'] = $element['#icon'];
}
if (isset($element['#id'])) {
if ($element['#id'] === 'full_screen_search') {
$variables['icon'] = TRUE;
}
}
$variables['#attached']['library'][] = 'dxpr_theme/drupal-search';
}
/**
* Generate web manifest file.
*/
function _dxpr_theme_generate_manifest($web_icons_media_id, $manifest_path) {
$config = \Drupal::config('system.site');
$name = $config->get('name') ?: 'Drupal Site';
$manifest = [
'name' => $name,
'short_name' => mb_strimwidth($name, 0, 12, ''),
'id' => '/',
'start_url' => '/',
'scope' => '/',
'display' => 'standalone',
'icons' => [],
];
$media = Media::load($web_icons_media_id);
if ($media && $media->hasField('field_media_image') && $media->field_media_image->entity) {
$file_uri = $media->field_media_image->entity->getFileUri();
// Add 192x192 icon.
$image_style_192 = ImageStyle::load('android_chrome_192');
if ($image_style_192) {
$url_192 = $image_style_192->buildUrl($file_uri);
$manifest['icons'][] = [
'src' => \Drupal::service('file_url_generator')->transformRelative($url_192),
'sizes' => '192x192',
'type' => 'image/png',
'purpose' => 'any maskable',
];
}
// Add 512x512 icon.
if ($image_style_512 = ImageStyle::load('android_chrome_512')) {
$url_512 = $image_style_512->buildUrl($file_uri);
$manifest['icons'][] = [
'src' => \Drupal::service('file_url_generator')->transformRelative($url_512),
'sizes' => '512x512',
'type' => 'image/png',
'purpose' => 'any maskable',
];
}
}
file_put_contents($manifest_path, json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
/**
* Implements template_preprocess_status_messages().
*/
function dxpr_theme_preprocess_status_messages(&$variables) {
$variables['#attached']['library'][] = 'dxpr_theme/drupal-status-message';
}
/**
* Implements template_preprocess_details()
*/
function dxpr_theme_preprocess_details(&$variables) {
$variables['attributes']['class'][] = 'form-item';
}
/**
* Implements template_theme_suggestions_form_alter()
*/
function dxpr_theme_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__' . $variables['element']['#form_id'];
}