3 namespace Drupal\Core\Controller;
5 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6 use Drupal\Core\Form\FormBuilderInterface;
7 use Drupal\Core\Form\FormState;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Symfony\Component\HttpFoundation\Request;
12 * Common base class for form interstitial controllers.
14 * @todo Make this a trait in PHP 5.4.
16 abstract class FormController {
17 use DependencySerializationTrait;
20 * The controller resolver.
22 * @var \Drupal\Core\Controller\ControllerResolverInterface
24 protected $controllerResolver;
29 * @var \Drupal\Core\Form\FormBuilderInterface
31 protected $formBuilder;
34 * Constructs a new \Drupal\Core\Controller\FormController object.
36 * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
37 * The controller resolver.
38 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
41 public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder) {
42 $this->controllerResolver = $controller_resolver;
43 $this->formBuilder = $form_builder;
47 * Invokes the form and returns the result.
49 * @param \Symfony\Component\HttpFoundation\Request $request
51 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
55 * The render array that results from invoking the controller.
57 public function getContentResult(Request $request, RouteMatchInterface $route_match) {
58 $form_arg = $this->getFormArgument($route_match);
59 $form_object = $this->getFormObject($route_match, $form_arg);
61 // Add the form and form_state to trick the getArguments method of the
62 // controller resolver.
63 $form_state = new FormState();
64 $request->attributes->set('form', []);
65 $request->attributes->set('form_state', $form_state);
66 $args = $this->controllerResolver->getArguments($request, [$form_object, 'buildForm']);
67 $request->attributes->remove('form');
68 $request->attributes->remove('form_state');
70 // Remove $form and $form_state from the arguments, and re-index them.
71 unset($args[0], $args[1]);
72 $form_state->addBuildInfo('args', array_values($args));
74 return $this->formBuilder->buildForm($form_object, $form_state);
78 * Extracts the form argument string from a request.
80 * Depending on the type of form the argument string may be stored in a
81 * different request attribute.
83 * One example of a route definition is given below.
86 * _form: Drupal\example\Form\ExampleForm
89 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
90 * The route match object from which to extract a form definition string.
93 * The form definition string.
95 abstract protected function getFormArgument(RouteMatchInterface $route_match);
98 * Returns the object used to build the form.
100 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
102 * @param string $form_arg
103 * Either a class name or a service ID.
105 * @return \Drupal\Core\Form\FormInterface
106 * The form object to use.
108 abstract protected function getFormObject(RouteMatchInterface $route_match, $form_arg);