Skip to content

Instantly share code, notes, and snippets.

@aklump
Last active August 22, 2018 06:43
Show Gist options
  • Save aklump/2a5fe6f1eb302a4fd88675bbcaf042a5 to your computer and use it in GitHub Desktop.
Save aklump/2a5fe6f1eb302a4fd88675bbcaf042a5 to your computer and use it in GitHub Desktop.
Programmatically embed a node or entity edit form in Drupal 8
// Without the use of D.I.
$form = \Drupal::service('entity.manager')
->getFormObject('node', 'default')
->setEntity($node);
$build[] = \Drupal::formBuilder()->getForm($form);
// Within a class using D.I.
class someClass {
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')
);
}
public function someFunction() {
$build = array();
$form = $this->entityManager
->getFormObject('node', 'default')
->setEntity($node);
$build[] = \Drupal::formBuilder()->getForm($form);
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment