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
  • 9526 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
  • 1755 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
  • 1093 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
  • 2251 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
  • 1713 View
  • 1 Score
  • 2 Answer
  • Tags:   theming 8

2 Answered Questions

Unable to show form using controller (module)

1 Answered Questions

[SOLVED] Send form result back to twig

  • 2017-01-08 14:24:06
  • JimmyD
  • 833 View
  • 3 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
  • 2234 View
  • 2 Score
  • 0 Answer
  • Tags:   theming

Sponsored Content