-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap_views_cards.module
85 lines (72 loc) · 2.95 KB
/
bootstrap_views_cards.module
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
<?php
/**
* @file
* Custom functions for Bootstrap Views Cards.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Template\Attribute;
/**
* Prepares variables for views cards templates.
*
* Default template: bootstrap-views-cards.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_bootstrap_views_cards(array &$vars): void {
$view = $vars['view'];
$id = $view->storage->id() . '-' . $view->current_display;
$vars['id'] = Html::getUniqueId('bootstrap-views-cards-' . $id);
$vars['attributes'] = new Attribute(['class' => ['row', 'g-3', 'row-cols-1']]);
$cols = $view->style_plugin->options['card_group_columns'];
if ($cols > 1) {
switch ($cols) {
case 2:
$vars['attributes']->addClass('row-cols-sm-2');
break;
case 3:
$vars['attributes']->addClass('row-cols-sm-2 row-cols-md-3');
break;
case 4:
$vars['attributes']->addClass('row-cols-sm-2 row-cols-md-4');
break;
case 5:
$vars['attributes']->addClass('row-cols-sm-3 row-cols-md-5');
break;
case 6:
$vars['attributes']->addClass('row-cols-sm-3 row-cols-md-6');
break;
}
}
$options = $view->style_plugin->options;
if ($options['card_group_class_custom']) {
$option_classes = array_filter(explode(' ', $options['card_group_class_custom']));
$classes = array_map([Html::class, 'cleanCssIdentifier'], $option_classes);
$vars['attributes']->addClass($classes);
}
$vars['image_option'] = $view->style_plugin->options['card_image_option_field'];
$bodyClass = $vars['image_option'] == 'background' ? 'card-img-overlay' : 'card-body';
$vars['body_attributes'] = new Attribute(['class' => [$bodyClass]]);
// Card rows.
$header = $view->style_plugin->options['card_header_field'];
$image = $view->style_plugin->options['card_image_field'];
$title = $view->style_plugin->options['card_title_field'];
$subtitle = $view->style_plugin->options['card_subtitle_field'];
$content = $view->style_plugin->options['card_body_field'];
$footer = $view->style_plugin->options['card_footer_field'];
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = [];
$vars['rows'][$id]['header'] = $view->style_plugin->getField($id, $header);
$vars['rows'][$id]['image'] = $view->style_plugin->getField($id, $image);
$vars['rows'][$id]['title'] = $view->style_plugin->getField($id, $title);
$vars['rows'][$id]['subtitle'] = $view->style_plugin->getField($id, $subtitle);
$vars['rows'][$id]['content'] = $view->style_plugin->getField($id, $content);
$vars['rows'][$id]['footer'] = $view->style_plugin->getField($id, $footer);
$vars['rows'][$id]['attributes'] = new Attribute(['class' => ['card', 'h-100']]);
if ($row_class = $view->style_plugin->getRowClass($id)) {
$vars['rows'][$id]['attributes']->addClass($row_class);
}
}
}