0

I am working with Drupal 8. I have a view page and added a twig for that.

I want to call a block inside my view page. How can I do that?

2 Answers 2

2

You can use module Twig Tweak this module you can render view,block ... You can see document here

Render views

{{ drupal_view('who_s_new', 'block_1') }}

Render blocks

{{ drupal_block(plugin_id) }}
2

Here is some possible ways to do it:

1. using no extra modules

Click ADD next to header or footer area and look for GLOBAL: Rendered entity - Block. The configuration asks for view mode and block id

enter image description here

2. using Views block area module

You can use Views block area module that will allow you to insert a block as a field or in header or footer area.

3. using preprocess / twig

if you want to add the block using your twig template in your THEMENAME.theme file using a preprocess function

function THEMENAME_preprocess_XXX (&$variables) {
    $block = \Drupal\block\Entity\Block::load('id_block');
    $variables['myBlock'] = \Drupal::entityTypeManager()
        ->getViewBuilder('block')
        ->view($block);
}

and then inside your twig file use this

{{ myBlock }}

To find appropriate preprocess function look in your twig file comments.

For example if you want to use views-view--unformatted.html.twig you can see in the comments

* @see template_preprocess_views_view_unformatted()

which means include in your .theme file THEMENAME_preprocess_views_view_unformatted() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.