Score:0

Programmatically render block content render array with wrappers

de flag

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')
  ->load($bid);

$output['header'] = \Drupal::entityTypeManager()
  ->getViewBuilder('block_content')
  ->view($block);

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?

No Sssweat avatar
ua flag
Not sure which wrappers you speak of. I don't see any difference in markup between placing a block and the code you have.
Jaypan avatar
de flag
@NoSssweat - see the duplicate thread. That was the solution.
4uk4 avatar
cn flag
You could add a #theme_wrappers, the problem here, the core `block` template is not a suitable template for this.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.