5 * Defines theming processors for views_responsive_grid.
8 use Drupal\Core\Template\Attribute;
11 * Process variables for views responsive grid style templates.
13 * Default template: views-view-responsive-grid.html.twig.
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.
21 function template_preprocess_views_view_responsive_grid(&$vars) {
22 drupal_add_css(drupal_get_path('module', 'views_responsive_grid') . '/views_responsive_grid.css');
24 $options = $vars['options'] = $vars['view']->style_plugin->options;
25 $horizontal = ($options['alignment'] === 'horizontal');
28 $vars['attributes'] = new Attribute(array(
30 'views-responsive-grid',
31 $options['alignment'],
32 'cols-' . $options['columns'],
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.
47 $items[$row][$col] = $item;
50 $items[$col][$row] = $item;
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);
60 // Add special views row classes.
61 if ($options['row_class_special']) {
64 $row_attributes['class'][] = 'first';
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';
71 $row_attributes['class'][] = (($row + 1) % 2) ? 'odd' : 'even';
74 $row_attributes['class'][] = 'clearfix';
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);
82 // Add row attributes to variables array.
84 $vars['row_attributes'][$row] = new Attribute($row_attributes);
87 $vars['row_attributes'][$col][$row] = new Attribute($row_attributes);
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);
97 // Add special views row classes.
98 if ($options['col_class_special']) {
100 $col_attributes['class'][] = 'first';
102 if ($col == $options['columns'] - 1 || ($row_count <= $num_rows && $col == $row_count)) {
103 $col_attributes['class'][] = 'last';
106 $col_attributes['class'][] = (($col + 1) % 2) ? 'odd' : 'even';
109 $col_attributes['class'][] = 'clearfix';
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);
117 // Add width to columns.
118 if ($options['columns'] > 1 && $options['automatic_width']) {
119 $col_attributes['style'] = 'width: ' . (100 / $options['columns']) . '%;';
121 // Add column attributes to variables array.
123 $vars['col_attributes'][$row][$col] = new Attribute($col_attributes);
126 $vars['col_attributes'][$col] = new Attribute($col_attributes);
129 // Increase, decrease or reset the appropriate integers.
134 elseif ($col >= ($options['columns'] - 1)) {
144 if (!$remainders && $row == $num_rows) {
148 elseif ($remainders && $row == $num_rows + 1) {
156 // Save the grid items to the variables array.
157 $vars['items'] = $items;