Similar to Drupal 7, Drupal 8 also gives the customization using theme template, If you want to place a block using programmatically you have following options :
1) Using the default Drupal method using THEME_preprocess_page,write this function in your mytheme.theme file, See the example below :
function THEME_preprocess_page(&$variables) {
// Load block
$block = \Drupal\block\Entity\Block::load('your_block_machine_name');
$variables['block_view'] = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
//Get block title
$block_title = \Drupal\block\Entity\Block::load('main_menu')->label();
$variables['title_of_block'] = $block_title;
}
and put "block_view
" & "title_of_block
" as this format in twig template
block title : {{title_of_block}}
block view : {{block_view}}
2) You can also try the easiest way using the Twig Blocks Module to make the variable directly, Just on the module and put the machine name like below:
{{ render_block('machine_name_of_your_block') }}
3) You can also try this module Twig Tweak, this also use to render views Blocks in theme
{{ drupal_block('machine_name_of_your_block','block_1') }}
Hope this helps you,
Thanks
- Log in to post comments