class FormController

Same name in this branch
  1. 10 core/modules/system/tests/modules/condition_test/src/FormController.php \Drupal\condition_test\FormController
Same name in other branches
  1. 9 core/modules/system/tests/modules/condition_test/src/FormController.php \Drupal\condition_test\FormController
  2. 9 core/lib/Drupal/Core/Controller/FormController.php \Drupal\Core\Controller\FormController
  3. 8.9.x core/modules/system/tests/modules/condition_test/src/FormController.php \Drupal\condition_test\FormController
  4. 8.9.x core/lib/Drupal/Core/Controller/FormController.php \Drupal\Core\Controller\FormController
  5. 11.x core/modules/system/tests/modules/condition_test/src/FormController.php \Drupal\condition_test\FormController
  6. 11.x core/lib/Drupal/Core/Controller/FormController.php \Drupal\Core\Controller\FormController

Common base class for form interstitial controllers.

Hierarchy

Expanded class hierarchy of FormController

4 files declare their use of FormController
HtmlEntityFormController.php in core/lib/Drupal/Core/Entity/HtmlEntityFormController.php
LayoutBuilderDecorationTestHtmlEntityFormController.php in core/modules/layout_builder/tests/modules/layout_builder_decoration_test/src/Controller/LayoutBuilderDecorationTestHtmlEntityFormController.php
LayoutBuilderHtmlEntityFormController.php in core/modules/layout_builder/src/Controller/LayoutBuilderHtmlEntityFormController.php
WorkspacesHtmlEntityFormController.php in core/modules/workspaces/src/Controller/WorkspacesHtmlEntityFormController.php

File

core/lib/Drupal/Core/Controller/FormController.php, line 15

Namespace

Drupal\Core\Controller
View source
abstract class FormController {
    use DependencySerializationTrait;
    
    /**
     * The argument resolver.
     *
     * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
     */
    protected $argumentResolver;
    
    /**
     * The form builder.
     *
     * @var \Drupal\Core\Form\FormBuilderInterface
     */
    protected $formBuilder;
    
    /**
     * Constructs a new \Drupal\Core\Controller\FormController object.
     *
     * @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
     *   The argument resolver.
     * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
     *   The form builder.
     */
    public function __construct(ArgumentResolverInterface $argument_resolver, FormBuilderInterface $form_builder) {
        $this->argumentResolver = $argument_resolver;
        $this->formBuilder = $form_builder;
    }
    
    /**
     * Invokes the form and returns the result.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request object.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     *
     * @return array
     *   The render array that results from invoking the controller.
     */
    public function getContentResult(Request $request, RouteMatchInterface $route_match) {
        $form_arg = $this->getFormArgument($route_match);
        $form_object = $this->getFormObject($route_match, $form_arg);
        // Add the form and form_state to trick the getArguments method of the
        // controller resolver.
        $form_state = new FormState();
        $request->attributes
            ->set('form', []);
        $request->attributes
            ->set('form_state', $form_state);
        $args = $this->argumentResolver
            ->getArguments($request, [
            $form_object,
            'buildForm',
        ]);
        $request->attributes
            ->remove('form');
        $request->attributes
            ->remove('form_state');
        // Remove $form and $form_state from the arguments, and re-index them.
        unset($args[0], $args[1]);
        $form_state->addBuildInfo('args', array_values($args));
        return $this->formBuilder
            ->buildForm($form_object, $form_state);
    }
    
    /**
     * Extracts the form argument string from a request.
     *
     * Depending on the type of form the argument string may be stored in a
     * different request attribute.
     *
     * One example of a route definition is given below.
     * @code
     *   defaults:
     *     _form: Drupal\example\Form\ExampleForm
     * @endcode
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match object from which to extract a form definition string.
     *
     * @return string
     *   The form definition string.
     */
    protected abstract function getFormArgument(RouteMatchInterface $route_match);
    
    /**
     * Returns the object used to build the form.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param string $form_arg
     *   Either a class name or a service ID.
     *
     * @return \Drupal\Core\Form\FormInterface
     *   The form object to use.
     */
    protected abstract function getFormObject(RouteMatchInterface $route_match, $form_arg);

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormController::$argumentResolver protected property The argument resolver.
FormController::$formBuilder protected property The form builder.
FormController::getContentResult public function Invokes the form and returns the result. 3
FormController::getFormArgument abstract protected function Extracts the form argument string from a request. 5
FormController::getFormObject abstract protected function Returns the object used to build the form. 5
FormController::__construct public function Constructs a new \Drupal\Core\Controller\FormController object. 5

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.