0

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

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

$form = \Drupal::entityTypeManager()
  ->getFormObject('node', 'default')
  ->setEntity($node);
$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?

1 Answer 1

1

I managed to use:

$html = drupal_render($form);

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

3
  • 1
    Why not let it render normally?
    – Kevin
    Commented Jan 3, 2019 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. Commented Jan 3, 2019 at 2:19
  • Just put the form variables to your Twig templates. They will be rendered automatically.
    – ya.teck
    Commented Jan 7, 2019 at 5:15

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.