By Vamsi


2016-07-18 09:08:46 8 Comments

In drupal 8 I want to print the block programatically. For this I have tried the below code but not working.

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

In page.html.twig I have added {{ page.block_output }}. But block is not rendered. Is there any other way to do this. Without using contrib modules.

2 comments

@No Sssweat 2016-07-18 09:23:25

Don't reinvent the wheel, you can use Twig tweak

Twig Tweak module provides a Twig extension with some useful functions and filters that can improve development experience.

Ex: <dd>{{ drupal_block('bartik_powered') }}</dd>

@tanc 2016-10-19 10:14:32

This has just made me very happy!

@Ajay Gadhavana 2017-07-06 11:59:53

Fast and easy solution.

@ProGrammar 2017-08-22 21:49:14

I'm trying to render the default search block (mytheme_search) and this is not working for that. Any idea?

@Yusef Mohamadi 2016-07-18 10:15:21

To render contributor module block's

function theme_preprocess_page(&$variables) {
  $block = \Drupal\block\Entity\Block::load('simplenewssubscription');
  $block_content = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);
 $variables['youblockname'] = $block_content;
}

and in your theme file just use

{{ youblockname }}

it seems you do it correct and just need use block_output instead page.block_output .

and rebuild the cache

@batigolix 2018-06-14 09:29:48

Note that this will make the variable your_block_name available in the page.html.twig . For other template files use the appropriate preprocess hook. E.g. theme_preprocess_node for node.html.twig files

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Entity print get variable in twig template

1 Answered Questions

How to add twig code in View 'global custom text' field?

  • 2017-03-28 08:51:27
  • rks
  • 910 View
  • 1 Score
  • 1 Answer
  • Tags:   views 8

2 Answered Questions

[SOLVED] Render menu block in twig template

  • 2017-01-14 08:58:31
  • meez
  • 2543 View
  • 1 Score
  • 2 Answer
  • Tags:   theming 8 blocks

2 Answered Questions

How to trim the Node field is rendered in page.html.twig?

  • 2017-08-09 11:13:16
  • logesh
  • 447 View
  • 0 Score
  • 2 Answer
  • Tags:   8

2 Answered Questions

[SOLVED] Unable to access Views fields in block Twig template

3 Answered Questions

[SOLVED] How do you print View fields on Twig?

  • 2016-04-27 01:55:42
  • No Sssweat
  • 7238 View
  • 5 Score
  • 3 Answer
  • Tags:   views theming 8

3 Answered Questions

[SOLVED] Programatically render a block in a twig template

  • 2015-03-29 13:41:06
  • Rick Bergmann
  • 35124 View
  • 26 Score
  • 3 Answer
  • Tags:   blocks 8

2 Answered Questions

[SOLVED] Twig theme variables

  • 2015-11-04 20:23:54
  • BrettW
  • 1304 View
  • 2 Score
  • 2 Answer
  • Tags:   8 theming

2 Answered Questions

[SOLVED] How to print block generated by views in the tpl file?

  • 2012-06-22 16:17:32
  • Eduardo Gabriel Cabrera Japa
  • 1660 View
  • 0 Score
  • 2 Answer
  • Tags:   6 blocks

Sponsored Content