bf8cfe93d880b88368287bf3aced87c2c383100e
[yaffs-website] / web / modules / contrib / views_responsive_grid / views_responsive_grid.theme.inc
1 <?php
2
3 /**
4  * @file
5  * Defines theming processors for views_responsive_grid.
6  */
7
8 use Drupal\Core\Template\Attribute;
9
10 /**
11  * Process variables for views responsive grid style templates.
12  *
13  * Default template: views-view-responsive-grid.html.twig.
14  *
15  * @param array $vars
16  *   An associative array containing:
17  *   - view: The view object.
18  *   - rows: An array of row items. Each row is an array of content.
19  *   - options: The view object style plugin options.
20  */
21 function template_preprocess_views_view_responsive_grid(&$vars) {
22   drupal_add_css(drupal_get_path('module', 'views_responsive_grid') . '/views_responsive_grid.css');
23   
24   $options = $vars['options'] = $vars['view']->style_plugin->options;
25   $horizontal = ($options['alignment'] === 'horizontal');
26
27   // View classes.
28   $vars['attributes'] = new Attribute(array(
29     'class' => array(
30       'views-responsive-grid',
31       $options['alignment'],
32       'cols-' . $options['columns'],
33       'clearfix',
34     ),
35   ));
36
37   // Setup integers.
38   $col = 0;
39   $row = 0;
40   $row_count = count($vars['rows']);
41   $remainders = $row_count % $options['columns'];
42   $num_rows = floor($row_count / $options['columns']);
43   // Iterate over the view rows array.
44   foreach ($vars['rows'] as $row_index => $item) {
45     // Add the current views data row to the rows array.
46     if ($horizontal) {
47       $items[$row][$col] = $item;
48     }
49     else {
50       $items[$col][$row] = $item;
51     }
52
53     // Create attributes for row.
54     $row_attributes = array('class' => array());
55     // Add default views row classes.
56     if ($options['default_row_class']) {
57       $row_attributes['class'][] = 'views-row';
58       $row_attributes['class'][] = 'row-' . ($row + 1);
59     }
60     // Add special views row classes.
61     if ($options['row_class_special']) {
62       // First row.
63       if ($row == 0) {
64         $row_attributes['class'][] = 'first';
65       }
66       // Last row.
67       if (($horizontal && $row == (!$remainders ? $num_rows - 1 : $num_rows)) || (!$horizontal && ((!$remainders && $row == $num_rows - 1) || ($remainders && $row == $num_rows)))) {
68         $row_attributes['class'][] = 'last';
69       }
70       // Zebra striping.
71       $row_attributes['class'][] = (($row + 1) % 2) ? 'odd' : 'even';
72       // Clearfix
73       if ($horizontal) {
74         $row_attributes['class'][] = 'clearfix';
75       }
76     }
77     // Add custom row class.
78     $row_class = array_filter(explode(' ', $options['row_class']));
79     if (!empty($row_class)) {
80       $row_attributes['class'] = array_merge($row_attributes['class'], $row_class);
81     }
82     // Add row attributes to variables array.
83     if ($horizontal) {
84       $vars['row_attributes'][$row] = new Attribute($row_attributes);
85     }
86     else {
87       $vars['row_attributes'][$col][$row] = new Attribute($row_attributes);
88     }
89   
90     // Create attributes for columns.
91     $col_attributes = array('class' => array());
92     // Add default views column classes.
93     if ($options['default_col_class']) {
94       $col_attributes['class'][] = 'views-col';
95       $col_attributes['class'][] = 'col-' . ($col + 1);
96     }
97     // Add special views row classes.
98     if ($options['col_class_special']) {
99       if ($col == 0) {
100         $col_attributes['class'][] = 'first';
101       }
102       if ($col == $options['columns'] - 1 || ($row_count <= $num_rows && $col == $row_count)) {
103         $col_attributes['class'][] = 'last';
104       }
105       // Zebra striping.
106       $col_attributes['class'][] = (($col + 1) % 2) ? 'odd' : 'even';
107       // Clearfix
108       if (!$horizontal) {
109         $col_attributes['class'][] = 'clearfix';
110       }
111     }
112     // Add custom column class.
113     $col_class = array_filter(explode(' ', $options['col_class']));
114     if (!empty($col_class)) {
115       $col_attributes['class'] = array_merge($col_attributes['class'], $col_class);
116     }
117     // Add width to columns.
118     if ($options['columns'] > 1 && $options['automatic_width']) {
119       $col_attributes['style'] = 'width: ' . (100 / $options['columns']) . '%;';
120     }
121     // Add column attributes to variables array.
122     if ($horizontal) {
123       $vars['col_attributes'][$row][$col] = new Attribute($col_attributes);
124     }
125     else {
126       $vars['col_attributes'][$col] = new Attribute($col_attributes);
127     }
128     
129     // Increase, decrease or reset the appropriate integers.
130     if ($horizontal) {
131       if ($col == 0) {
132         $col++;
133       }
134       elseif ($col >= ($options['columns'] - 1)) {
135         $col = 0;
136         $row++;
137       }
138       else {
139         $col++;
140       }
141     }
142     else {
143       $row++;
144       if (!$remainders && $row == $num_rows) {
145         $row = 0;
146         $col++;
147       }
148       elseif ($remainders && $row == $num_rows + 1) {
149         $row = 0;
150         $col++;
151         $remainders--;
152       }
153     }
154     $row_count--;
155   }
156   // Save the grid items to the variables array.
157   $vars['items'] = $items;
158 }