Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Controller / FormController.php
1 <?php
2
3 namespace Drupal\Core\Controller;
4
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;
10
11 /**
12  * Common base class for form interstitial controllers.
13  *
14  * @todo Make this a trait in PHP 5.4.
15  */
16 abstract class FormController {
17   use DependencySerializationTrait;
18
19   /**
20    * The controller resolver.
21    *
22    * @var \Drupal\Core\Controller\ControllerResolverInterface
23    */
24   protected $controllerResolver;
25
26   /**
27    * The form builder.
28    *
29    * @var \Drupal\Core\Form\FormBuilderInterface
30    */
31   protected $formBuilder;
32
33   /**
34    * Constructs a new \Drupal\Core\Controller\FormController object.
35    *
36    * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
37    *   The controller resolver.
38    * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
39    *   The form builder.
40    */
41   public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder) {
42     $this->controllerResolver = $controller_resolver;
43     $this->formBuilder = $form_builder;
44   }
45
46   /**
47    * Invokes the form and returns the result.
48    *
49    * @param \Symfony\Component\HttpFoundation\Request $request
50    *   The request object.
51    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
52    *   The route match.
53    *
54    * @return array
55    *   The render array that results from invoking the controller.
56    */
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);
60
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');
69
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));
73
74     return $this->formBuilder->buildForm($form_object, $form_state);
75   }
76
77   /**
78    * Extracts the form argument string from a request.
79    *
80    * Depending on the type of form the argument string may be stored in a
81    * different request attribute.
82    *
83    * One example of a route definition is given below.
84    * @code
85    *   defaults:
86    *     _form: Drupal\example\Form\ExampleForm
87    * @endcode
88    *
89    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
90    *   The route match object from which to extract a form definition string.
91    *
92    * @return string
93    *   The form definition string.
94    */
95   abstract protected function getFormArgument(RouteMatchInterface $route_match);
96
97   /**
98    * Returns the object used to build the form.
99    *
100    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
101    *   The route match.
102    * @param string $form_arg
103    *   Either a class name or a service ID.
104    *
105    * @return \Drupal\Core\Form\FormInterface
106    *   The form object to use.
107    */
108   abstract protected function getFormObject(RouteMatchInterface $route_match, $form_arg);
109
110 }