How I can pass a form to twig template in Drupal 8?

For example, I get this:

a form: mymodule/src/Form/MyForm.php

Then on my Controller:

public function myController() {


    return [
        '#theme' => 'mytemplate',


{{ ????? }}
  • Do you mean you'd like the entity object available in the preprocess/template, or you'd like the rendered view of the entity available?
    – Clive
    Feb 4 '15 at 17:20
  • @Clive I want the rendered view of the entity available for send to template and then this show it.
    – rpayanm
    Feb 4 '15 at 18:12
  • 1
    There is no such thing as a "render entity". nodes and blocks have a similar API to get displayed, but forms are quite different and definitely aren't an entity. Ask a specific question and you will get an answer that will help you. what kind of entity, do you just want to view with the default output, or use it in your own template, ...
    – Berdir
    Feb 4 '15 at 18:25
  • Let me then edit the question.
    – rpayanm
    Feb 4 '15 at 21:32
  • That's still not enough information. So again, what kind of entity do you want to output? And what is your template doing exactly. In short, what is your input and expected output. Also include your hook_theme() implementation.
    – Berdir
    Feb 5 '15 at 16:17

Well, I did it, for anyone that needs this:

public function pageAction() {
  $myform = \Drupal::formBuilder()->getForm('Drupal\[mymodule]\Form\[myformclass]');
  // If you want modify the form:
  $myform['field']['#value'] = 'From my controller';

  $build = [
    '#theme' => 'mytemplate'
    '#form' => $myform,

In twig:

{{ form }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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