To load the login form I am using the following code.

$form = \Drupal::formBuilder()->getForm('Drupal\user\Form\UserLoginForm');

I would like to load the form to create or edit a node. How can I achieve this?

1 Answer 1


Copying partially from an answer that I gave here: How to alter $form_state before render a form?

To get an add form, you can have to do something like this:

$values = array('type' => $node_type);

$node = \Drupal::entityTypeManager()

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

Edit: Somehow I missed that you said edit. You do exactly the same then, except that you don't create a new $node, you load the one you want to edit and then use that.

As written there, this is based on the entityblock_form module, so if you need this in a block, you can also just use that module.

  • Thanks for your help, I have implemented same for profile form, it works, Along with address fields I have the country, stage fields, which is an Ajax enabled. form alter is disturbing on ajax call, I want to call a function on #after_build, but it's not invoking. please help Jun 16, 2017 at 10:14
  • 1
    If you are in a controller, there's an easier way to get the node add form. After creating the new node object as in Berdir's code above, you can simply use: $form_render_array = $this->entityFormBuilder()->getForm($node);
    – Giles B
    Oct 22, 2017 at 21:38
  • Awesome hint, kudos to Berdir Nov 21, 2017 at 17:25
  • Just to be clear, the above should be done in the Controller. I tried to do it extending FormBase for way too long.
    – kosher
    Jul 14, 2020 at 23:00
  • +1 doing it via controller I am able to load the form just fine, thank you!
    – Boby
    Jul 23, 2020 at 19:40

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.