It’s not uncommon to have pages on a Drupal site with very specific layouts, requiring custom twig templating. But what do you do if you need to place a block with automated content between content-managed fields in the same region? How do you place that block within your twig template?
In this post I’m going to show you how to create a Twig variable for both a Views block and a custom block to place in your theme’s twig templates.
Views Block Twig Variable
In order to create a Twig variable of a Views block, we’ll need a preprocess function like this:
function mymodule_preprocess_node(&$variables) {
$variables['VARIABLE_NAME'] = views_embed_view('VIEW_MACHINE_NAME', 'BLOCK_MACHINE NAME');
}
You can find the machine name of the view on the views admin page: