Drupal8: Set block in a region programmatically.

This CodeLet will display a block content in a region programmatically.
You have to implement hook_preprocess_HOOK() in THEMENAME.theme file of your theme directory.

CodeLet: 

/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Add block in a region.
*/

function THEMENAME_preprocess_html(&$variables) {

$block = \Drupal\block\Entity\Block::load('YOUR_BLOCK_ID');
$block_content = \Drupal::entityManager()
->getViewBuilder('block')
->view($block);

$variables['page']['YOUR REGION'][] = $block_content;
}

By saru1683 on Mon, 04/01/2016 - 10:10am

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback