I'm using the following code to render a set of blocks programmatically:
$region_content = [];
$block_manager = \Drupal::service('plugin.manager.block');
$blocks = $this->getBlocks();
foreach ($blocks[$region_name] as $key=>$block_config) {
$instance = $block_manager->createInstance($block_config['plugin_id'], $block_config['config']);
$region_content[] = [
$instance->build();
];
}
The contents of $block_config['config'] look like this:
[
'provider' => 'search',
'admin_label' => '',
'label' => 'Search form',
'label_display' => TRUE,
'third_party_settings' => [
'block_aria_landmark_roles' => [
'role' => '',
],
],
]
The resulting $instance object seems to work correctly, and when I render $region_content I do indeed get the contents of the blocks as expected. But they are not wrapped in the block template. I thought I might have to do something like this:
$region_content[] = [
'#theme' => 'block',
'contents' => $instance->build(),
];
...but that doesn't work at all. How do I take this $instance object and render it using the standard block template? Or am I completely off course here?