0

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);
//kint($block);
    $block_view = \Drupal::entityManager()->getViewBuilder('block_content')->view($block);
    $variables['page']['content_right']['custom_block'] = $block_view;
  }
}

What am I missing?

1 Answer 1

0

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, [])
  ->build();

$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

Your Answer

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