How to Embed a View in a Node in Drupal 9

When it comes to embedding a view in Drupal, there are many choices available. Normally I would recommend creating a display block in the View and adding the block to the page, but in some cases, you may want or need to embed the view in a template. These are two ways you can do that:

  1. Preprocessing the view and rendering it in twig:

The first step is to add this to your *.theme file in your theme. In this example, I’m making sure the view is only loaded in full view mode and only in content types landing page and campaign page. I needed to preset the arguments with the values selected by the editor so $string_category can have a string value like “Press” and $array_tags is an array of term ids, for example [1, 2, 3].
The view will be available in Twig using the variable blog_filter_block.

/*** Implements template_preprocess_node().* @param $variables*/function THEME_preprocess_node(&$variables) {  if ($variables['view_mode'] == 'full' && in_array($variables['node']->bundle(), ['landing_page', 'campaign_page'])) {    $blog_filter_block  = views_embed_view('blog', 'blog_filter_block', $string_category, $array_tags);    $variables["blog_filter_block"] = \Drupal::service('renderer')->renderRoot($blog_filter_block);  }}

In node.html.twig, add the code below. If you don’t have that template in your theme yet, you can copy it from core/modules/node/templates to your theme folder (don’t forget to clear the cache).

{% if blog_filter_block is not empty %}  {{ blog_filter_block }}{% endif %}

2. Adding it directly in twig using twig tweak module:

To embed the view this way you will need to install first the module Twig Tweak. In your THEME_preprocess_node function, you will only have to create the variables you will need in the twig template, but not the view:

$variables["blog_filter_category"] = $string_category;$variables["blog_filter_tags"] = $array_tags;

Then in node.html.twig you can render the view using the function drupal_view:

{% if (blog_filter_category != 'all' or blog_filter_tags != 'all') %}  {{ drupal_view('blog', 'blog_filter_block', blog_filter_category, blog_filter_tags) }}{% endif %}

You can ignore the if condition. I had to use it to make sure both values were not empty.

Don’t forget that when doing changes in twig templates, you may have to clear the cache to see your changes.

--

--

--

I write articles in English and Spanish, mostly about programming, technology, travel, money, investing. You can find me here: https://sergioguardiola.net

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Effectively Testing Our AWS S3 Utilization Using The S3Mock

The Impersonal Code Review

Try, Try, and Try Again

CS373 Fall 2021: Nathan Eisenberg — Blog 4

Galaxy Roll First Release!

A/B Testing Mastery

navigation

What is CURL?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sergio Guardiola Herrador

Sergio Guardiola Herrador

I write articles in English and Spanish, mostly about programming, technology, travel, money, investing. You can find me here: https://sergioguardiola.net

More from Medium

How to Lazy-Load Images in Drupal

Getting Drupal 10 Ready Before its Launch: Important Tips

Unlimited Free Email Address using Cloudflare and your Free Custom Dom