All of the existing examples in the form_api_example module display the form as the content of a route. However, sometimes you need to display the form elsewhere. This is done by using the form builder service and inserting the returned render array into the page wherever you want the form to display.
For example, the user login block. Or the search form block.
This adds a block to the form_api_example module that displays the Drupal\form_api_example\Form\SimpleForm in the block. Doing so demonstrates how to use the form builder service to retrieve the render array representation of a form and have it displayed.
Comment | File | Size | Author |
---|---|---|---|
#4 | interdiff.txt | 3.06 KB | Mile23 |
#4 | 2945435_4.patch | 5.8 KB | Mile23 |
| |||
form_api_example-block.patch | 5.28 KB | eojthebrave | |
|
Comments
Comment #2
Mile23This would be a nice addition.
Is there a way to show this form handling validation and submit? The test will need an update to verify that it works.
Let's wrap this so it's easier to read as twig.
Comment #3
eojthebraveValidation and submit both work without any additions. So, I'm not really what else there is to show in the code other than maybe adding to the comments that someone should fill out the form to see it in action. There's already a test that verifies the form displays in the block. And, I feel like adding tests for whether submitForm works might just replicate the tests already in place for the SimpleForm form.
E.g.:
Though, if you think it's worthwhile to have independent tests for the form being submitted from the block I can write those. Thoughts?
Comment #4
Mile23Minor improvements to description text, CS, converted class name string literals.
Comment #6
Mile23And done. Thanks!