Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a form using block module in drupal 8?

I want build a form using a block module in Drupal 8. I am aware of building the forms in Drupal 7 but the same seems to be different in Drupal 8.

Request anyone who has worked on drupal8 custom forms as block to help me.

like image 804
user3664791 Avatar asked Apr 15 '16 09:04

user3664791


People also ask

How to render a form into a block in Drupal 8?

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 * Contains \Drupalesume\Form\WorkForm. Step 2: Now lets display the form we created in step 1 as block.

How to create a custom block library in Drupal 8?

Since blocks have their own dedicated page in Drupal 8 (and it's similar to any content page), where you get all the entities of type “block” listed (it uses views to display them all)! So “ How it's done? ” you say? Step 1: you navigate to your Custom Block Library from your toolbar menu: Structure -> Block Layout -> Custom Block Library .

How to create a module in Drupal 8?

Look here: Naming and placing your Drupal 8 module. Basically you create the module folder and the module info yml file to let Drupal know about the module. Then you enable it using drush or the admin area in Drupal.

How to create a form programatically in a custom block?

To build a form using block module, you can easily use Webform module where you can add a form and display as a block. If you mean to create a form programatically in the custom block, you can achieve that by creating two files shown below:


1 Answers

Your question is very vague, as I don't know how much you already know about modules, forms and blocks in Drupal 8. So here is a small guide what to do, further information on how to do stuff in detail would be overkill for this answer.

1. Create a new module and enable it

Look here: Naming and placing your Drupal 8 module.

Basically you create the module folder and the module info yml file to let Drupal know about the module. Then you enable it using drush or the admin area in Drupal.

2. Create the form

Look here: Introduction to Form API.

under your_module/src/Form you create the form. More details in the link above.

3. Create the block and render the form

Look here: Create a custom block.

under your_module/src/Plugin/Block/ you create the block which will render the form.

The idea is basically (code updated with suggestion from Henrik):

$builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\Your‌​Form');
$renderArray['form'] = $builtForm;

return $renderArray;

Note: You don't need to wrap the $builtForm with the $renderArray, you can return just the $builtForm and be fine. I just personally like to do it that way, because often times I need to add something else to the final render array like some markup, cache settings or a library etc.

4. Place the block

Place the block in the desired region(s). Done.

like image 173
Frank Drebin Avatar answered Sep 20 '22 14:09

Frank Drebin