3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Controller\ControllerResolverInterface;
6 use Drupal\Core\Controller\FormController;
7 use Drupal\Core\Form\FormBuilderInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
11 * Wrapping controller for entity forms that serve as the main page body.
13 class HtmlEntityFormController extends FormController {
16 * The entity manager service.
18 * @var \Drupal\Core\Entity\EntityManagerInterface
20 protected $entityManager;
23 * Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object.
25 * @param \Drupal\Core\Controller\ControllerResolverInterface $resolver
26 * The controller resolver.
27 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
29 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
32 public function __construct(ControllerResolverInterface $resolver, FormBuilderInterface $form_builder, EntityManagerInterface $manager) {
33 parent::__construct($resolver, $form_builder);
34 $this->entityManager = $manager;
40 protected function getFormArgument(RouteMatchInterface $route_match) {
41 return $route_match->getRouteObject()->getDefault('_entity_form');
47 * Instead of a class name or service ID, $form_arg will be a string
48 * representing the entity and operation being performed.
49 * Consider the following route:
51 * path: '/foo/{node}/bar'
53 * _entity_form: 'node.edit'
55 * This means that the edit form for the node entity will used.
56 * If the entity type has a default form, only the name of the
57 * entity {param} needs to be passed:
59 * path: '/foo/{node}/baz'
61 * _entity_form: 'node'
64 protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
65 // If no operation is provided, use 'default'.
66 $form_arg .= '.default';
67 list ($entity_type_id, $operation) = explode('.', $form_arg);
69 $form_object = $this->entityManager->getFormObject($entity_type_id, $operation);
71 // Allow the entity form to determine the entity object from a given route
73 $entity = $form_object->getEntityFromRouteMatch($route_match, $entity_type_id);
74 $form_object->setEntity($entity);