I have a set of views (in D7.57, views 7.x-3.18). Each view takes a selection of my (drupal commerce) product-displays and places them in a grid, in alphabetical order.
Now I want to add a bespoke item as first item (so in row 1, column 1), 'bumping up' all normal products one place (so product 1 goes to row 1 column 2, etc). Whatever I try (adding global header in views (obviously wrong), changing the views-grid-xx.tpl
) 'breaks' the rows, displaying my bespoke item in perfect order, but breaking the row.
Option 1: adapt the views style or row output .tpl
Update after @AltaGrade answer: Thanks for your answer. That's what I tried, I amended both the 'style' and the 'row' templates for the view, but do not get the desired results with either.
The view does display my insert (which is a piece of fixed code, same lay-out etc as the normal grid items), but then treats the normal generated items as a full row, causing breaks where I don't want them, and also repeats my insert each row.
My rows (of three columns each) then look like:
insert - item 1 - item 2
item 3
insert - item 4 - item 5
and so on..
Code of the views.grid.xxx.tpl
looks like this, where and how would I code my inserts (one to go before the first item in row one; one to go after all items have been listed)?
<?php
/**
* @file
* Default simple view template to display a rows in a grid.
*
* - $rows contains a nested array of rows. Each row contains an array of columns.
*
* @ingroup views_templates
*/
// Match Column numbers to Bootsrap class
$columns_classes = array(1 => 12, 2 => 6, 3 => 4, 4 => 3, 6 => 2, 12 => 1);
$bootsrap_class = isset($columns_classes[$view->style_plugin- >options['columns']]) ? $columns_classes[$view->style_plugin- >options['columns']] : 3;
$delay_attr = '';
?>
<?php foreach ($rows as $row_number => $columns): ?>
<div class = "row<?php if ($row_classes[$row_number]) { print ' ' . $row_classes[$row_number]; } ?>" >
<?php foreach ($columns as $column_number => $item): ?>
<?php
if(isset($column_classes[$row_number][$column_number]) && strpos($column_classes[$row_number][$column_number], 'animation-delay') !== FALSE) {
$delay = isset($delay) ? $delay + 0.1 : 0.1;
$delay_attr = ' data-wow-delay = "' . $delay . 's" ';
}
?>
<div class = "col-md-<?php print $bootsrap_class; ?><?php if ($column_classes[$row_number][$column_number]) { print ' ' . $column_classes[$row_number][$column_number]; } ?>" <?php print $delay_attr; ?>>
<?php print $item; ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
Option 2: Preprocess the view output using pre_render
.
One suggestion I found is using hooks (hook_views_pre_render
, hook_views_post_render
?), is this the best way? If so, where would I put such a hook? Are these actually written into views.api.php
? And how do I then write such a hook that it only applies to the view I want? All pointers / help very welcome, hooks 100% new area for me.
In the views.api.php
I also found a whole section on views-handlers
which looks similar to the hooks, are these an option too?.
Option 3: create extra content types
Create new content types (in this case product types) and let the view render first extra type, then the main products, then the second extra type. So I'd create a new content type for the bespoke items, and then add them to the view's filter. Any explanation how I would do that so it neatly merges into the row would be amazing.