-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_bootstrap.theme.inc
229 lines (210 loc) · 7.73 KB
/
views_bootstrap.theme.inc
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
<?php
/**
* @file
* Preprocessors and helper functions to make theming easier.
*/
use Drupal\views_bootstrap\ViewsBootstrap;
use Drupal\Core\Template\Attribute;
/**
* Prepares variables for views accordion templates.
*
* Default template: views-bootstrap-accordion.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_accordion(array &$vars) {
$view = $vars['view'];
$vars['id'] = ViewsBootstrap::getUniqueId($view);
$panel_title_field = $view->style_plugin->options['panel_title_field'];
$vars['attributes']['class'][] = 'panel-group';
if ($panel_title_field) {
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = [];
$vars['rows'][$id]['content'] = $row;
$vars['rows'][$id]['title'] = $view->style_plugin->getFieldValue($id, $panel_title_field);
}
}
else {
// @TODO: This would be better as valdiation errors on the style plugin options form.
drupal_set_message(t('@style style will not display without the "@field" setting.',
[
'@style' => $view->style_plugin->definition['title'],
'@field' => 'Panel title',
]
), 'warning');
}
// @TODO: Make sure that $vars['rows'] is rendered array.
// @SEE: Have a look template_preprocess_views_view_unformatted()
// and views-view-unformatted.html.twig
}
/**
* Prepares variables for views carousel template.
*
* Default template: views-bootstrap-carousel.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_carousel(array &$vars) {
$view = $vars['view'];
$vars['id'] = ViewsBootstrap::getUniqueId($view);
$vars['attributes']['class'][] = 'views-bootstrap-media-object';
$vars['attributes']['class'][] = 'media-list';
// Carousel options.
$vars['interval'] = $view->style_plugin->options['interval'];
$vars['navigation'] = $view->style_plugin->options['navigation'];
$vars['indicators'] = $view->style_plugin->options['indicators'];
$vars['pause'] = $view->style_plugin->options['pause'] ? 'hover' : FALSE;
$vars['wrap'] = $view->style_plugin->options['wrap'];
// Carousel rows.
$image = $view->style_plugin->options['image'];
$title = $view->style_plugin->options['title'];
$description = $view->style_plugin->options['description'];
$fieldLabels = $view->display_handler->getFieldLabels(TRUE);
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = [];
$vars['rows'][$id]['image'] = $view->style_plugin->getField($id, $image);
$vars['rows'][$id]['title'] = $view->style_plugin->getField($id, $title);
$vars['rows'][$id]['description'] = $view->style_plugin->getField($id, $description);
// Add any additional fields to result.
foreach (array_keys($fieldLabels) as $label) {
if (!in_array($label, [$image, $title, $description])) {
$vars['rows'][$id][$label] = $view->style_plugin->getField($id, $label);
}
}
}
}
/**
* Prepares variables for views grid templates.
*
* Default template: views-bootstrap-grid.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_grid(array &$vars) {
$view = $vars['view'];
$vars['id'] = ViewsBootstrap::getUniqueId($view);
$vars['attributes']['class'][] = 'grid';
$options = $view->style_plugin->options;
$options['automatic_width'] = ['default' => TRUE];
$horizontal = ($options['alignment'] === 'horizontal');
foreach (['xs', 'sm', 'md', 'lg'] as $size) {
$vars["col_" . $size] = $options["col_" . $size];
// Get the value from the size sting.
$vars['sizes'][$size] = ViewsBootstrap::getColSize($options["col_" . $size]);
}
$vars['options'] = $options;
}
/**
* Prepares variables for views list group templates.
*
* Default template: views-bootstrap-list-group.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_list_group(array &$vars) {
$view = $vars['view'];
$options = $view->style_plugin->options;
$vars['id'] = ViewsBootstrap::getUniqueId($view);
$vars['attributes']['class'][] = 'views-bootstrap-list-group';
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = [];
$vars['rows'][$id]['content'] = $row;
$vars['rows'][$id]['title'] = $vars['view']->style_plugin->getField($id, $options['title_field']);
}
}
/**
* Prepares variables for views media object templates.
*
* Default template: views-bootstrap-media-object.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_media_object(array &$vars) {
$vars['id'] = ViewsBootstrap::getUniqueId($vars['view']);
$image_class = $vars['view']->style_plugin->options['image_class'];
$image_field = $vars['view']->style_plugin->options['image_field'];
$heading_field = $vars['view']->style_plugin->options['heading_field'];
$body_field = $vars['view']->style_plugin->options['body_field'];
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = [];
$vars['classes'][$id] .= ' media-object';
$vars['rows'][$id]['image_class'] = $image_class;
$vars['rows'][$id]['image'] = $vars['view']->style_plugin->getField($id, $image_field);
$vars['rows'][$id]['heading'] = $vars['view']->style_plugin->getField($id, $heading_field);
$vars['rows'][$id]['body'] = $vars['view']->style_plugin->getField($id, $body_field);
}
}
/**
* Prepares variables for views tab templates.
*
* Default template: views-bootstrap-tab.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_tab(array &$vars) {
$vars['id'] = ViewsBootstrap::getUniqueId($vars['view']);
$view = $vars['view'];
$tab_field = $view->style_plugin->options['tab_field'];
$vars['tab_type'] = $view->style_plugin->options['tab_type'];
$vars['justified'] = $view->style_plugin->options['justified'];
// Get tabs.
if ($tab_field) {
if (isset($view->field[$tab_field])) {
foreach (array_keys($vars['rows']) as $key) {
$vars['tabs'][$key] = $view->style_plugin->getFieldValue($key, $tab_field);
}
}
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = array();
$vars['rows'][$id]['content'] = $row;
$vars['rows'][$id]['attributes'] = new Attribute();
if ($row_class = $view->style_plugin->getRowClass($id)) {
$vars['rows'][$id]['attributes']->addClass($row_class);
}
}
}
else {
// @TODO: This would be better as valdiation errors on the style plugin options form.
drupal_set_message(t('@style style will not display without the "@field" setting.',
[
'@style' => $view->style_plugin->definition['title'],
'@field' => 'Tab title',
]
), 'warning');
}
}
/**
* Prepares variables for views table templates.
*
* Default template: views-bootstrap-table.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_bootstrap_table(array &$vars) {
$vars['responsive'] = $vars['view']->style_plugin->options['responsive'];
$vars['attributes']['class'][] = 'table';
foreach (array_filter($vars['view']->style_plugin->options['bootstrap_styles']) as $style) {
$vars['attributes']['class'][] = 'table-' . $style;
}
}