By rpayanm


2015-02-04 17:05:29 8 Comments

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',
        ??????
        ];
}

mytemplate.html.twig

{{ ????? }}

1 comments

@rpayanm 2015-03-08 14:29:39

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 }}

Related Questions

Sponsored Content

2 Answered Questions

Pass variables from controller to existing node template file

4 Answered Questions

[SOLVED] Adding autocomplete for text-field

  • 2016-05-04 06:07:38
  • make-me-alive
  • 8750 View
  • 11 Score
  • 4 Answer
  • Tags:   8 forms

0 Answered Questions

Pager not rendering in custom twig template

1 Answered Questions

[SOLVED] pass variable from custom module file to twig template in d8

  • 2018-04-24 06:14:16
  • Ashish
  • 1553 View
  • 0 Score
  • 1 Answer
  • Tags:   8

1 Answered Questions

[SOLVED] how to add form action twig template form

  • 2017-12-02 07:04:48
  • gulshan
  • 913 View
  • 0 Score
  • 1 Answer
  • Tags:   8 forms

1 Answered Questions

[SOLVED] How to pass a custom Form to a Twig template

  • 2017-11-24 11:33:44
  • gulshan
  • 1905 View
  • 0 Score
  • 1 Answer
  • Tags:   theming 8 forms

2 Answered Questions

[SOLVED] Drupal 8 - How to add link & display in twig template

  • 2017-10-03 04:19:40
  • Ankit Shah
  • 1456 View
  • 1 Score
  • 2 Answer
  • Tags:   theming 8

0 Answered Questions

Pass custom variable to twig

  • 2017-03-14 19:03:51
  • Yusef Mohamadi
  • 2487 View
  • -1 Score
  • 0 Answer
  • Tags:   theming 8 routes

1 Answered Questions

[SOLVED] Send form result back to twig

  • 2017-01-08 14:24:06
  • JimmyD
  • 785 View
  • 4 Score
  • 1 Answer
  • Tags:   8 forms

0 Answered Questions

How do I use a custom twig template with a render element?

  • 2016-06-16 15:41:15
  • Ian Carnaghan
  • 2126 View
  • 2 Score
  • 0 Answer
  • Tags:   theming

Sponsored Content