Last active
August 22, 2018 06:43
-
-
Save aklump/2a5fe6f1eb302a4fd88675bbcaf042a5 to your computer and use it in GitHub Desktop.
Programmatically embed a node or entity edit form in Drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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