5 * Preprocessors and helper functions to make theming easier.
8 use Drupal\views_bootstrap\ViewsBootstrap;
9 use Drupal\Core\Template\Attribute;
12 * Prepares variables for views accordion templates.
14 * Default template: views-bootstrap-accordion.html.twig.
17 * An associative array containing:
18 * - view: A ViewExecutable object.
19 * - rows: The raw row data.
21 function template_preprocess_views_bootstrap_accordion(array &$vars) {
22 $view = $vars['view'];
23 $vars['id'] = ViewsBootstrap::getUniqueId($view);
24 $panel_title_field = $view->style_plugin->options['panel_title_field'];
25 $vars['attributes']['class'][] = 'panel-group';
26 if ($panel_title_field) {
27 foreach ($vars['rows'] as $id => $row) {
28 $vars['rows'][$id] = [];
29 $vars['rows'][$id]['content'] = $row;
30 $vars['rows'][$id]['title'] = $view->style_plugin->getFieldValue($id, $panel_title_field);
34 // @TODO: This would be better as valdiation errors on the style plugin options form.
35 drupal_set_message(t('@style style will not display without the "@field" setting.',
37 '@style' => $view->style_plugin->definition['title'],
38 '@field' => 'Panel title',
42 // @TODO: Make sure that $vars['rows'] is rendered array.
43 // @SEE: Have a look template_preprocess_views_view_unformatted()
44 // and views-view-unformatted.html.twig
48 * Prepares variables for views carousel template.
50 * Default template: views-bootstrap-carousel.html.twig.
53 * An associative array containing:
54 * - view: A ViewExecutable object.
55 * - rows: The raw row data.
57 function template_preprocess_views_bootstrap_carousel(array &$vars) {
58 $view = $vars['view'];
59 $vars['id'] = ViewsBootstrap::getUniqueId($view);
60 $vars['attributes']['class'][] = 'views-bootstrap-media-object';
61 $vars['attributes']['class'][] = 'media-list';
64 $vars['interval'] = $view->style_plugin->options['interval'];
65 $vars['navigation'] = $view->style_plugin->options['navigation'];
66 $vars['indicators'] = $view->style_plugin->options['indicators'];
67 $vars['pause'] = $view->style_plugin->options['pause'] ? 'hover' : FALSE;
68 $vars['wrap'] = $view->style_plugin->options['wrap'];
71 $image = $view->style_plugin->options['image'];
72 $title = $view->style_plugin->options['title'];
73 $description = $view->style_plugin->options['description'];
74 $fieldLabels = $view->display_handler->getFieldLabels(TRUE);
76 foreach ($vars['rows'] as $id => $row) {
77 $vars['rows'][$id] = [];
78 $vars['rows'][$id]['image'] = $view->style_plugin->getField($id, $image);
79 $vars['rows'][$id]['title'] = $view->style_plugin->getField($id, $title);
80 $vars['rows'][$id]['description'] = $view->style_plugin->getField($id, $description);
81 // Add any additional fields to result.
82 foreach (array_keys($fieldLabels) as $label) {
83 if (!in_array($label, [$image, $title, $description])) {
84 $vars['rows'][$id][$label] = $view->style_plugin->getField($id, $label);
92 * Prepares variables for views grid templates.
94 * Default template: views-bootstrap-grid.html.twig.
97 * An associative array containing:
98 * - view: A ViewExecutable object.
99 * - rows: The raw row data.
101 function template_preprocess_views_bootstrap_grid(array &$vars) {
102 $view = $vars['view'];
103 $vars['id'] = ViewsBootstrap::getUniqueId($view);
104 $vars['attributes']['class'][] = 'grid';
105 $options = $view->style_plugin->options;
106 $options['automatic_width'] = ['default' => TRUE];
107 $horizontal = ($options['alignment'] === 'horizontal');
109 foreach (['xs', 'sm', 'md', 'lg'] as $size) {
110 $vars["col_" . $size] = $options["col_" . $size];
111 // Get the value from the size sting.
112 $vars['sizes'][$size] = ViewsBootstrap::getColSize($options["col_" . $size]);
114 $vars['options'] = $options;
119 * Prepares variables for views list group templates.
121 * Default template: views-bootstrap-list-group.html.twig.
124 * An associative array containing:
125 * - view: A ViewExecutable object.
126 * - rows: The raw row data.
128 function template_preprocess_views_bootstrap_list_group(array &$vars) {
129 $view = $vars['view'];
130 $options = $view->style_plugin->options;
131 $vars['id'] = ViewsBootstrap::getUniqueId($view);
132 $vars['attributes']['class'][] = 'views-bootstrap-list-group';
133 foreach ($vars['rows'] as $id => $row) {
134 $vars['rows'][$id] = [];
135 $vars['rows'][$id]['content'] = $row;
136 $vars['rows'][$id]['title'] = $vars['view']->style_plugin->getField($id, $options['title_field']);
142 * Prepares variables for views media object templates.
144 * Default template: views-bootstrap-media-object.html.twig.
147 * An associative array containing:
148 * - view: A ViewExecutable object.
149 * - rows: The raw row data.
151 function template_preprocess_views_bootstrap_media_object(array &$vars) {
152 $vars['id'] = ViewsBootstrap::getUniqueId($vars['view']);
153 $image_class = $vars['view']->style_plugin->options['image_class'];
154 $image_field = $vars['view']->style_plugin->options['image_field'];
155 $heading_field = $vars['view']->style_plugin->options['heading_field'];
156 $body_field = $vars['view']->style_plugin->options['body_field'];
158 foreach ($vars['rows'] as $id => $row) {
159 $vars['rows'][$id] = [];
160 $vars['classes'][$id] .= ' media-object';
161 $vars['rows'][$id]['image_class'] = $image_class;
162 $vars['rows'][$id]['image'] = $vars['view']->style_plugin->getField($id, $image_field);
163 $vars['rows'][$id]['heading'] = $vars['view']->style_plugin->getField($id, $heading_field);
164 $vars['rows'][$id]['body'] = $vars['view']->style_plugin->getField($id, $body_field);
169 * Prepares variables for views tab templates.
171 * Default template: views-bootstrap-tab.html.twig.
174 * An associative array containing:
175 * - view: A ViewExecutable object.
176 * - rows: The raw row data.
178 function template_preprocess_views_bootstrap_tab(array &$vars) {
179 $vars['id'] = ViewsBootstrap::getUniqueId($vars['view']);
180 $view = $vars['view'];
181 $tab_field = $view->style_plugin->options['tab_field'];
182 $vars['tab_type'] = $view->style_plugin->options['tab_type'];
183 $vars['justified'] = $view->style_plugin->options['justified'];
187 if (isset($view->field[$tab_field])) {
188 foreach (array_keys($vars['rows']) as $key) {
189 $vars['tabs'][$key] = $view->style_plugin->getFieldValue($key, $tab_field);
192 foreach ($vars['rows'] as $id => $row) {
193 $vars['rows'][$id] = array();
194 $vars['rows'][$id]['content'] = $row;
195 $vars['rows'][$id]['attributes'] = new Attribute();
196 if ($row_class = $view->style_plugin->getRowClass($id)) {
197 $vars['rows'][$id]['attributes']->addClass($row_class);
202 // @TODO: This would be better as valdiation errors on the style plugin options form.
203 drupal_set_message(t('@style style will not display without the "@field" setting.',
205 '@style' => $view->style_plugin->definition['title'],
206 '@field' => 'Tab title',
213 * Prepares variables for views table templates.
215 * Default template: views-bootstrap-table.html.twig.
218 * An associative array containing:
219 * - view: A ViewExecutable object.
220 * - rows: The raw row data.
222 function template_preprocess_views_bootstrap_table(array &$vars) {
223 $vars['responsive'] = $vars['view']->style_plugin->options['responsive'];
224 $vars['attributes']['class'][] = 'table';
225 foreach (array_filter($vars['view']->style_plugin->options['bootstrap_styles']) as $style) {
226 $vars['attributes']['class'][] = 'table-' . $style;