HtmlEntityFormController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Controller\ControllerResolverInterface;
  4. use Drupal\Core\Controller\FormController;
  5. use Drupal\Core\Form\FormBuilderInterface;
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. /**
  8. * Wrapping controller for entity forms that serve as the main page body.
  9. */
  10. class HtmlEntityFormController extends FormController {
  11. /**
  12. * The entity manager service.
  13. *
  14. * @var \Drupal\Core\Entity\EntityManagerInterface
  15. */
  16. protected $entityManager;
  17. /**
  18. * Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object.
  19. *
  20. * @param \Drupal\Core\Controller\ControllerResolverInterface $resolver
  21. * The controller resolver.
  22. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
  23. * The form builder.
  24. * @param \Drupal\Core\Entity\EntityManagerInterface $manager
  25. * The entity manager.
  26. */
  27. public function __construct(ControllerResolverInterface $resolver, FormBuilderInterface $form_builder, EntityManagerInterface $manager) {
  28. parent::__construct($resolver, $form_builder);
  29. $this->entityManager = $manager;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function getFormArgument(RouteMatchInterface $route_match) {
  35. return $route_match->getRouteObject()->getDefault('_entity_form');
  36. }
  37. /**
  38. * {@inheritdoc}
  39. *
  40. * Instead of a class name or service ID, $form_arg will be a string
  41. * representing the entity and operation being performed.
  42. * Consider the following route:
  43. * @code
  44. * path: '/foo/{node}/bar'
  45. * defaults:
  46. * _entity_form: 'node.edit'
  47. * @endcode
  48. * This means that the edit form for the node entity will used.
  49. * If the entity type has a default form, only the name of the
  50. * entity {param} needs to be passed:
  51. * @code
  52. * path: '/foo/{node}/baz'
  53. * defaults:
  54. * _entity_form: 'node'
  55. * @endcode
  56. */
  57. protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
  58. // If no operation is provided, use 'default'.
  59. $form_arg .= '.default';
  60. list ($entity_type_id, $operation) = explode('.', $form_arg);
  61. $form_object = $this->entityManager->getFormObject($entity_type_id, $operation);
  62. // Allow the entity form to determine the entity object from a given route
  63. // match.
  64. $entity = $form_object->getEntityFromRouteMatch($route_match, $entity_type_id);
  65. $form_object->setEntity($entity);
  66. return $form_object;
  67. }
  68. }