Why isn't my custom block rendering in the region I want? It shows in the content region, not my custom content_right region.

function bootstrap_preprocess_html(&$variables) {
  if (\Drupal::routeMatch()->getRouteName() == 'some.path') {
    $block = \Drupal::entityManager()->getStorage('block_content')->load(12);
    $block_view = \Drupal::entityManager()->getViewBuilder('block_content')->view($block);
    $variables['page']['content_right']['custom_block'] = $block_view;

What am I missing?


You can try this, create an instance of a block plugin on the fly for the block content:

$block_content = \Drupal::entityTypeManager()->getStorage('block_content')->load(12);

$plugin_id = 'block_content:' . $block_content->uuid();
$block_view = \Drupal::service('plugin.manager.block')
  ->createInstance($plugin_id, [])

$variables['page']['content_right']['custom_block'] = $block_view;

But it's better to configure the block plugin in UI and build the configured block instead. For more info see this question Programatically render a block in a twig template

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy