0

Render form in Drupal 8.

In modulename.routing.yml

modulename.fp:

path: '/formpage'

defaults:

_controller: '\Drupal\modulename\Controller\*Controller::formpage'

Write code in controller.php

public function formpage() {

$form = '';

$form = 'Form Title';

$form .= \Drupal::formBuilder()->getForm('\Drupal\modulename\Form\fp');

return [

  '#type' => 'markup',

  '#markup' => $form,

];

}

and write form code in modules\modulename\src\Form\fp.php.

but showing Form Title Array

how to render form.

1 Answer 1

2

First of all why use a _controller if all you need to do is print a form? Instead it should have _form something like this: Drupal\example\Form\DefaultForm in the routing definition.

Here is a simple guide on how to create a form in drupal 8 https://www.valuebound.com/resources/blog/step-by-step-method-to-create-a-custom-form-in-drupal-8

And if you really want to embed a form in a controller response check this out How do I embed a form in a controller response?

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.