1

I'm able to load node create forms like so

$node    = \Drupal\node\Entity\Node::create(['type' => 'avenue']);
$content = \Drupal::service('entity.form_builder')->getForm($node);

return $content;

However what if I want to load an edit form for a pre-existing node? I looked on this page https://api.drupal.org/api/drupal/core!modules!node!src!Entity!Node.php/class/Node/8.2.x but I can't find anything that would relate to the edit form. Is there a way to load the edit form for a specific node?

1
  • 1
    Isn't it the same? As in, pass an existing $node object to getForm?
    – Clive
    Commented Dec 29, 2016 at 20:18

2 Answers 2

4

Yes, on the linked page you would find Node::load() to get a pre-existing node:

$node    = \Drupal\node\Entity\Node::load($nid);
$content = \Drupal::service('entity.form_builder')->getForm($node);

return $content;
1

In Controller -

if ($node) && !empty($node->id())) {
  $node = \Drupal\node\Entity\Node::load($node->id());
  $build = \Drupal::service('entity.form_builder')->getForm($node);
  return $build;
}
else {
  return $this->redirect('node.add', ['node_type' => 'CONTENT_TYPE']);
}

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.