I'm trying to render a custom block, created through the UI, programmatically with a wrapper. I can do this:

$block = \Drupal::entityTypeManager()->getStorage('block_content')

$output['header'] = \Drupal::entityTypeManager()

And the block is rendered, but only the fields of the block - the block wrappers are not rendered.

I have tried adding:

$output['header']['#theme_wrappers'][] = 'block';

This leads to errors:

Warning: Undefined array key "#configuration" in [...]/block.module on line 200

Warning: Undefined array key "#plugin_id" in [...]/block.module on line 212


So how can I programmatically render a block_content entity with block wrappers?

  • Not sure which wrappers you speak of. I don't see any difference in markup between placing a block and the code you have.
    – No Sssweat
    Apr 18, 2023 at 22:02
  • @NoSssweat - see the duplicate thread. That was the solution.
    – Jaypan
    Apr 19, 2023 at 0:03
  • You could add a #theme_wrappers, the problem here, the core block template is not a suitable template for this.
    – 4uk4
    Apr 19, 2023 at 6:40


Browse other questions tagged or ask your own question.