$bid = 'myblock';
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
Creating a block instance
$block_manager = \Drupal::service('plugin.manager.block');
$config = [];
$plugin_block = $block_manager->createInstance('system_breadcrumb_block', $config);
$access_result = $plugin_block->access(\Drupal::currentUser());
if (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result) {
return [];
}
$render = $plugin_block->build();
Rendering an already placed block (even in a disabled region) and providing it to a template.
$block_id = 'my_block_id';
$block = \Drupal::entityTypeManager()
->getStorage('block')
->load($block_id);
if (!empty($block)) {
$block_content = $block->getPlugin()->build();
$variables['custom_block'] = $block_content;
}
Rendering an already placed block (even in a disabled region) and providing it to a template.
$block_id = 'my_block_id';
$block = \Drupal::entityTypeManager()
->getStorage('block')
->load($block_id);
if (!empty($block)) {
$block_view = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
$variables['custom_block'] = $block_view;
}
function my_module_get_blocks_by_region($region) {
$blocks = \Drupal::entityTypeManager()
->getStorage('block')->loadByProperties([
'theme' => \Drupal::theme()->getActiveTheme()->getName(),
'region' => $region,
]);
uasort($blocks, 'Drupal\block\Entity\Block::sort');
$build = [];
foreach ($blocks as $key => $block) {
if ($block->access('view')) {
$build[$key] = entity_view($block, 'block');
}
}
return $build;
}