I have a node type called "competition". I can get a form for it like so:

$values = array('type' => 'competition');
$node = \Drupal::entityTypeManager()

$form = \Drupal::entityTypeManager()
  ->getFormObject('node', 'default')
$form = \Drupal::formBuilder()->getForm($form);

In a controller it's easy enough to return $form and it will be rendered. But I want to do other things with this form so I need it back as HTML. How can I turn $form into fully rendered html?


I managed to use:

$html = drupal_render($form);

Just like you would do in Drupal 7. Not great, but solves my problem.

|improve this answer|||||
  • 1
    Why not let it render normally? – Kevin Jan 3 '19 at 2:18
  • I want to place different node creation forms in different tabs in my custom html. I could probably use "field groups" but I ran into a wall with a few issues. SO hacking them all into my own custom html code is just a lot easier. – coderama Jan 3 '19 at 2:19
  • Just put the form variables to your Twig templates. They will be rendered automatically. – ya.teck Jan 7 '19 at 5:15

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.