Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / views_bootstrap / views_bootstrap.theme.inc
1 <?php
2
3 /**
4  * @file
5  * Preprocessors and helper functions to make theming easier.
6  */
7
8 use Drupal\views_bootstrap\ViewsBootstrap;
9 use Drupal\Core\Template\Attribute;
10
11 /**
12  * Prepares variables for views accordion templates.
13  *
14  * Default template: views-bootstrap-accordion.html.twig.
15  *
16  * @param array $vars
17  *   An associative array containing:
18  *   - view: A ViewExecutable object.
19  *   - rows: The raw row data.
20  */
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->getField($id, $panel_title_field);
31     }
32   }
33   else {
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.',
36       [
37         '@style' => $view->style_plugin->definition['title'],
38         '@field' => 'Panel title',
39       ]
40     ), 'warning');
41   }
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
45 }
46
47 /**
48  * Prepares variables for views carousel template.
49  *
50  * Default template: views-bootstrap-carousel.html.twig.
51  *
52  * @param array $vars
53  *   An associative array containing:
54  *   - view: A ViewExecutable object.
55  *   - rows: The raw row data.
56  */
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';
62
63   // Carousel options.
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'];
69
70   // Carousel rows.
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);
75
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);
85       }
86     }
87   }
88
89 }
90
91 /**
92  * Prepares variables for views grid templates.
93  *
94  * Default template: views-bootstrap-grid.html.twig.
95  *
96  * @param array $vars
97  *   An associative array containing:
98  *   - view: A ViewExecutable object.
99  *   - rows: The raw row data.
100  */
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');
108
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]);
113   }
114   $vars['options'] = $options;
115
116 }
117
118 /**
119  * Prepares variables for views list group templates.
120  *
121  * Default template: views-bootstrap-list-group.html.twig.
122  *
123  * @param array $vars
124  *   An associative array containing:
125  *   - view: A ViewExecutable object.
126  *   - rows: The raw row data.
127  */
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']);
137   }
138
139 }
140
141 /**
142  * Prepares variables for views media object templates.
143  *
144  * Default template: views-bootstrap-media-object.html.twig.
145  *
146  * @param array $vars
147  *   An associative array containing:
148  *   - view: A ViewExecutable object.
149  *   - rows: The raw row data.
150  */
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'];
157
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);
165   }
166 }
167
168 /**
169  * Prepares variables for views tab templates.
170  *
171  * Default template: views-bootstrap-tab.html.twig.
172  *
173  * @param array $vars
174  *   An associative array containing:
175  *   - view: A ViewExecutable object.
176  *   - rows: The raw row data.
177  */
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'];
184
185   // Get tabs.
186   if ($tab_field) {
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);
190       }
191     }
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);
198       }
199     }
200   }
201   else {
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.',
204       [
205         '@style' => $view->style_plugin->definition['title'],
206         '@field' => 'Tab title',
207       ]
208     ), 'warning');
209   }
210 }
211
212 /**
213  * Prepares variables for views table templates.
214  *
215  * Default template: views-bootstrap-table.html.twig.
216  *
217  * @param array $vars
218  *   An associative array containing:
219  *   - view: A ViewExecutable object.
220  *   - rows: The raw row data.
221  */
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;
227   }
228 }
229