Dependency Injection for a Form

Last updated on
25 March 2017

Forms that require a Drupal service or a custom service should access the service using dependency injection.

An example form (similar to the form used in Form API in Drupal 8) uses the 'current_user' service to get the uid of the current user. File contents of /modules/example/src/Form/ExampleForm.php if the module is in /modules/example:

<?php
/**
 * @file
 * Contains \Drupal\example\Form\ExampleForm.
 */
namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Implements an example form.
 */
class ExampleForm extends FormBase {
  /**
   * @var AccountInterface $account
   */
  protected $account;

  /**
   * Class constructor.
   */
  public function __construct(AccountInterface $account) {
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Instantiates this form class.
    return new static(
      // Load the service required to construct this class.
      $container->get('current_user')
    );
  }

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'example_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get current user data.
    $uid = $this->account->id();
    
    // ...
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // ...
  }
}
?>

The create method is a factory method that returns a new instance of the ExampleForm object. The ExampleForm::create loads one or more services. This can be any core service, defined in core.services.yml or any *.services.yml file.

ExampleForm::__construct uses the services that are loaded by ExampleForm::create and stores them in properties of the class. The order in which the services are loaded in ExampleForm::create must be equal to the order of the parameters in the ExampleForm::__construct method.

The create method is part of the Drupal\Core\DependencyInjection\ContainerInjectionInterface which allows controllers to be instantiated with a service. Drupal\Core\Form\FormBase already implements this interface. Any form that extends Drupal\Core\Form\FormBase, such as ConfigFormBase and ConfirmFormBase, has this ability of Dependency Injection.

<?php

/**
 * @file
 * Contains \Drupal\example\Form\ExampleConfigForm.
 */
namespace Drupal\example\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;

/**
 * Implements an example configuration form.
 */
class ExampleConfigForm extends ConfigFormBase {

  /**
   * Drupal\Core\Session\AccountProxy definition.
   *
   * @var AccountProxy $currentUser
   */
  protected $currentUser;

  /**
   * Class constructor.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AccountProxy $current_user) {
    parent::__construct($config_factory);
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('current_user')
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'example.config_form',
    ];
  }

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'example_config_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get current user data.
    $uid = $this->currentUser->id();
    drupal_set_message($uid);

    // ...

    $config = $this->config('example.config_form');
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('example.config_form')->save();
    parent::submitForm($form, $form_state);
  }
}

Add how FormController can auto automatically insert $route info into your buildInfo() method by simply defining a parameter with the same class interface.

Example

buildForm(array $form, FormStateInterface $form_state, Request $request = NULL)