Custom form block in Drupal 8
Blog

Creating a custom form in a block in two steps in Drupal 8

Lot of times we come across project requirement where standards form created using contact form or webform is not sufficient or overkill. In this article I am sharing the flexibility of Drupal 8 where I am creating a basic form and rendering the same form into a block

Step 1: Lets create a custom form - Add a file in resume/src/Form/WorkForm.php

Step 2: Now lets display the form we created in step 1 as block. For this create a new file  article/Plugin/Block/ArticleBlock.php

FormBuilder::getForm($form_arg)

This function should be used instead of self::buildForm() when $form_state is not needed (i.e., when initially rendering the form) and is often used as a menu callback.

Now you can check your page output “Article Block” is displayed in right side on the website.

s5

Source code: https://github.com/xaiwant/D8custom-form-rendring-in-block

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch