For a site I'm currently building I've got a variety of views, all with multiple display modes for different pages.
For example, I have a venues
view, one block is named listing
which lists all of my venues
content type. I have another block which has some more specific contextual filters.
My issue is, this different block is styled the same, and has the same markup as my listing
block so I don't want to have to duplicate my Twig templates for the rows and fields, for each display mode I have.
What my idea was is to add a function to my .theme
file that would allow me to list display modes for a certain view, and add a single theme suggestion off that back of that. This way I can rename my Twig templates, and re-use the same ones for row and field level, on as many display modes as I'd like.
As I'm not using a theme suggestions alter hook, I'm not sure how I actually add theme suggestions.
Here's what I've got so far:
/**
* Theme suggestions for views.
*/
function spve_preprocess_views_view(&$variables) {
$view = $variables['view'];
$id = $view->storage->id();
// Venues
if($id === 'venues') {
// Venues grid
switch($view->current_display) {
case 'single_occasion_type_listing':
$suggestion = 'views-view--venues-grid';
break;
default:
$suggestion = null;
break;
}
}
}
Obviously I'd need to replace either $suggestion
with the correct function to add a theme suggestion, or add a bit at the end of my function to add a theme suggestion based of my $suggestion
variable.