Rendering Plugin Blocks the Right Way in Drupal with Views and Webform
Christian Sanders of ComputerMinds explores how to programmatically render plugin blocks in Drupal using the Block Plugin API. While Drupal supports content, config, and plugin blocks, this article zeroes in on plugin blocks—those typically defined in src/Plugin/Block and extending BlockBase.
Sanders shares his experience implementing multiple blocks—two Views blocks and one Webform block—by determining their $plugin_id values. For Views, block IDs must follow the format views_block:view_machine_name-view_display, a detail hidden in the plugin’s deriver logic. For Webform blocks, the ID is simply webform_block, but it requires passing a configuration array including the webform_id.
Once the block instance is created using createInstance(), developers can call build() to get the render array and then add configurations like block label, theme wrapper, and attributes. These render arrays can be assigned to variables and printed in templates.
- Plugin ID pattern:
views_block:view_id-display_id - Webform blocks require
webform_idin config - Use
createInstance()+build()to get render arrays - Watch out for missing hooks like
hook_block_view() - Manual access checks and caching may be necessary
- Use Drush or Twig Tweak for discovery and debugging
Read the full tutorial on Rendering Plugin Blocks the Right Way to see complete code examples and detailed explanations.
