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

I managed to use:

$html = drupal_render($form);

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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