Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Views Bootstrap 5.5.4 — Minor update available for module views_bootstrap (5.5.4). Release: Rightup theme 1.0.3 — Minor update available for theme vartheme_bs5_rightup (1.0.3). Release: CommentOn 1.0.6 — Minor update available for module commenton (1.0.6). Release: ChatGPT Ads 1.0.3 — Minor update available for module chatgpt_ads (1.0.3). Release: Opensolr Search 5.1.3 — Minor update available for module opensolr_search (5.1.3). Release: ServiceM8 Webform Integration 1.2.1 — Minor update available for module servicem8_webform (1.2.1). Release: PostfixAdmin 1.0.0 — Initial release available for module postfix_admin (1.0.0)! Release: ip_analyser 1.0.3 — Minor update available for module ip_analyser (1.0.3). Module Revived: Swagger-PHP OpenAPI 3 documentation generator 1.0.0 — Module swagger_php updated after 10 months of inactivity (1.0.0). Security Coverage: Microsoft Azure AI — Module ai_provider_azure now has official Drupal security advisory coverage.

This module aims to make it easier to render blocks programatically in Drupal 8.

If you wish to render a block programatically, you already need a Block Entity instance in the block layout to be able to render it elsewhere. This means that you would need to hide your instances in some region, never to be printed or instantly disable them after creating, just so you could render them elsewhere in your code.

Another option would be to render the Block Plugins themselves. But by bypassing the Block Entities, your output won't be put through a theming function and therefore you lose the wrapper DIV for theming and contextual links also won't be rendered.

This module tries to solve this by implementing a theming function of its own based on the block twig template. This way we don't need to have any Block Entity instances in the block layout and can still get a wrapper DIV, including contextual links related to the block.

To use this, you need to call the block_renderer service and use the appropriate function to render your block.

$blockRenderer = \Drupal::service('block_renderer');
// Pass the ID of the block plugin you wish to render.
$plugin_block = $blockRenderer->renderPluginBlock(‘plugin.id');
return $plugin_block;

A separate function has been made to render Block Content entities, so you'd be able to just pass the Block Content ID instead of its more complicated UUID that would be needed when trying to render it by using the corresponding Block Plugin.

$blockRenderer = \Drupal::service('block_renderer');
// Pass the ID of the block content you wish to render.
$content_block = $blockRenderer->renderContentBlock(‘content.id');
return $content_block;

If you wish to provide your own attributes (HTML classes), you can provide a config array with those values.

$blockRenderer = \Drupal::service('block_renderer');

$config = [
  '#attributes' => [
    'class' => ['block-info'],
  ],
];
// Pass the ID of the block content you wish to render + additional config.
$content_block = $blockRenderer->renderContentBlock(‘content.id', $config);
return $content_block;

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Sep 2023
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Slowing

Releases

Version Type Core Release date
8.x-1.0-alpha6 Pre-release Apr 19, 2024
8.x-1.0-alpha5 Pre-release Sep 4, 2023