8

In drupal 7 we used to attach a webform to nodes and then we could render them as a block, nowadays in drupal 8 Webform is a dependent entity and is designed and developed on its own. now my question is :

I've created a webform and now I want to place it in a block so user can simply fill it up in front page, How can I show a webform as a Block?

1
  • 9
    By default, Webform block will be available in the block layout, once you created the webform, Just place the webform block in the any region. and then configure it and choose the particular webform which you created Feb 1, 2018 at 7:30

4 Answers 4

15

Special thanks to Peacog Web Development for their great tutorial .Here is the tip:

Rather than Drupal 7, Different Block types can be created by admin

so the solution is simple :

  • Create a new block type using : admin/structure/block/block-content/types
  • Add a Reference of type Webform to custom block type
  • remove body if it's inappropriate
  • modify custom block Manage Display so the form would be rendered as webform
  • your new Block type is ready

now to add any new webform as block you, you will need to add new block of type Custom Block and refer to your webform, and that's done.

3
  • Using this method allowed me to embed this into a node using Entity Embed, which I couldn't do otherwise.
    – neuquen
    Jul 16, 2019 at 19:30
  • 1
    logeshvaran/eigentor are correct, there is no need to create a custom block type.
    – wranvaud
    Jul 27, 2021 at 18:26
  • Downvoting because this answer "reinvents the wheel", duplicating functionality that comes with the Webform module, which contains the WebformBlock class.
    – mbomb007
    Nov 29, 2022 at 22:01
11

logeshvaran is correct: Webform for Drupal 8 already provides a block that can reference a webform. So no need to create a custom block type.

2
  • 3
    Make sure to search for "Webform" when placing the blocks, and then choose the Webform name in the dialog box provided. I've been searching for the name of the webform, that's why I couldn't find the block to place.
    – Randell
    Jun 7, 2019 at 1:12
  • 1
    Thank you @Randell! To expound on his comment. The Block name is "Webform" and the Category is "Webform". After you select that block you will be able to choose which webform you want.
    – neuquen
    Jul 16, 2019 at 18:13
8

From Drupal 8 admin

  1. Create your webform
  2. Structure => Block Layout => click "Place Block" under your preferred region
  3. Find Webform then => Place Block => under Webform type your webform title and it will find it
  4. Save Block

You are all set

1
  • 1
    Thanks @hzak for this straight-forward and step by step answer! Very useful, and quick to follow. Mar 26, 2021 at 4:15
0

You can embed a webform programmatically:

$webform = $this->entityTypeManager->getStorage('webform')->load(self::WEBFORM_ID);
$build['content'][] = [
  '#type' => 'webform',
  '#webform' => $webform,
];

Your Answer

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

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