I'm attempting to render a webform inside a block plugin. I'm able to successfully retrieve the webform using the following code

$webform = \Drupal::entityTypeManager()->getStorage('webform')->load('my_webform');

However I'd like to render this form. I've attempted using

return $webform;

But I receive the error

Recoverable fatal error: Argument 1 passed to Drupal\Core\Render\Element::isEmpty() must be of the type array, object given

I'm assuming this is because $webform isn't formatted as a proper render array, but I'm not sure how to do this. I've been searching for clues inside the code of the original webform module, but I haven't been able to find anything.

How would I programmatically render a webform? And/or is there a way to find in the code of a module or entity the way to render that entity?

4 Answers 4


I managed to find a method for the webform entity class called getSubmissionForm. I found it in the webform module code in src/Entity/Webform.php

I invoked it like so

$webform = \Drupal::entityTypeManager()->getStorage('webform')->load('my_webform');
$webform = $webform->getSubmissionForm();

return $webform;

Which returned the specified webform rendered.


You can also just return a render array.

  '#type' => 'webform',
  '#webform' => 'my_webform',
  '#default_data' => [
    'nid' => 10

Matt answers works for me but if you want to have a more complete theme structure with it, you would better use :

$webform      = \Drupal::entityTypeManager()->getStorage('webform')->load('my_webform');
$view_builder = \Drupal::service('entity_type.manager')->getViewBuilder('webform');
$build        = $view_builder->view($webform);
return $build;

To render this webform as a render array and to add it to your output


To render a Webform inside a Block plugin, you can use Block Token module for that. It will allow you to create the tokens for your blocks.

To create a webform block, go to Block Layout, then Place a block, then check the Create the token for this block checkbox for the block. Now the specific webform should be available as a block.

Here is the example to manually render the above created webform block:

$block = BlockViewBuilder::lazyBuilder('mywebform', 'full', 1);
$block_content = render($block);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.