Insert block into any template (drupal insert block into template)
Drupal 8
In a Twig template, you can't call PHP code directly, so you need to pass a ready-made block into the template. If you need to pass a variable into a node template, you can use the themename_preprocess_node()
function in your theme. For the page template, use themename_preprocess_page()
. Pass the variable to the Twig template, and then output it there.
Blocks created via the admin interface
$block = \Drupal\block\Entity\Block::load('your_block_id'); $variables['block_output'] = \Drupal::entityTypeManager() ->getViewBuilder('block') ->view($block);
In the themename_preprocess_node()
function (for a node template), this block can be rendered as follows:
function themename_preprocess_node(&$variables) { $block = \Drupal\block\Entity\Block::load('your_block_id'); $variables['block_output'] = \Drupal::entityTypeManager() ->getViewBuilder('block') ->view($block); }
In the node.html.twig
template, you can now render the block like this:
{{ block_output }}
Blocks created via plugins
These blocks can be rendered similarly to those created via the admin UI, but the rendering code differs slightly:
$block_manager = \Drupal::service('plugin.manager.block'); // You can define block-specific configuration here. $config = []; $plugin_block = $block_manager->createInstance('system_breadcrumb_block', $config); // Some blocks may have access restrictions. $access_result = $plugin_block->access(\Drupal::currentUser()); // Returns an empty array if access is denied. if ($access_result->isForbidden()) { return []; } $render = $plugin_block->build(); // You may need to add cache tags or contexts here. // Also consider adding cache tags or contexts in the render() method, ::getCacheTags, or ::getCacheContexts. return $render;
The same code without comments:
$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 ($access_result->isForbidden()) { return []; } $render = $plugin_block->build(); return $render;
Drupal 7
For Drupal 7, the function is similar, but the parameters differ slightly. Instead of two parameters, block
and view
, we use one: block_view
.
print render(module_invoke('block', 'block_view', '12'));
To insert a views block, it looks like this:
print render(module_invoke('views', 'block_view', 'feedbacks-block'));
This is how we insert a views block in Drupal 7. Note that for the first block, we omit the block number in the delta and just write feedbacks-block
.
Drupal 6
To insert a block where needed, use the module_invoke()
function:
$block = module_invoke('views', 'block', 'view', 'block_name'); print $block['content'];
For example, to insert a block created via the admin UI, use the following code:
$block = module_invoke('block', 'block', 'view', 12); print $block['content'];
Where 12
is the block's delta ID.
To insert a views block, do it like this:
$block = module_invoke('views', 'block', 'view', 'feedbacks-block_1'); print $block['content'];
Where feedbacks-block_1
is the block name in the view: feedbacks
is the view name, and block_1
is the display name within that view.