1. 8.5.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  2. 8.0.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  3. 8.1.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  4. 8.2.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  5. 8.3.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  6. 8.4.x core/lib/Drupal/Core/Form/FormBase.php FormBase
  7. 8.6.x core/lib/Drupal/Core/Form/FormBase.php FormBase

Provides a base class for forms.

This class exists as a mid-point between dependency injection through ContainerInjectionInterface, and a less-structured use of traits which default to using the \Drupal accessor for service discovery.

To properly inject services, override create() and use the setters provided by the traits to inject the needed services.

public static function create($container) {
  $form = new static();
  // In this example we only need string translation so we use the
  // setStringTranslation() method provided by StringTranslationTrait.
  return $form;

Alternately, do not use FormBase. A class can implement FormInterface, use the traits it needs, and inject services from the container as required.


Expanded class hierarchy of FormBase

See also


Related topics

137 files declare their use of FormBase
ActionAdminManageForm.php in core/modules/action/src/Form/ActionAdminManageForm.php
AjaxCssForm.php in core/modules/ckeditor/tests/modules/src/Form/AjaxCssForm.php
AjaxFormsTestAjaxElementsForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php
AjaxFormsTestCommandsForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCommandsForm.php
AjaxFormsTestImageButtonForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestImageButtonForm.php

... See full list


core/lib/Drupal/Core/Form/FormBase.php, line 44


View source
abstract class FormBase implements FormInterface, ContainerInjectionInterface {
  use DependencySerializationTrait;
  use LinkGeneratorTrait;
  use LoggerChannelTrait;
  use MessengerTrait;
  use RedirectDestinationTrait;
  use StringTranslationTrait;
  use UrlGeneratorTrait;

   * The request stack.
   * @var \Symfony\Component\HttpFoundation\RequestStack
  protected $requestStack;

   * The config factory.
   * Subclasses should use the self::config() method, which may be overridden to
   * address specific needs when loading config, rather than this property
   * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
   * this.
   * @var \Drupal\Core\Config\ConfigFactoryInterface
  protected $configFactory;

   * The route match.
   * @var \Drupal\Core\Routing\RouteMatchInterface
  protected $routeMatch;

   * {@inheritdoc}
  public static function create(ContainerInterface $container) {
    return new static();

   * {@inheritdoc}
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Validation is optional.

   * Retrieves a configuration object.
   * This is the main entry point to the configuration API. Calling
   * @code $this->config('book.admin') @endcode will return a configuration
   * object in which the book module can store its administrative settings.
   * @param string $name
   *   The name of the configuration object to retrieve. The name corresponds to
   *   a configuration file. For @code \Drupal::config('book.admin') @endcode,
   *   the config object returned will contain the contents of book.admin
   *   configuration file.
   * @return \Drupal\Core\Config\ImmutableConfig
   *   A configuration object.
  protected function config($name) {
    return $this

   * Gets the config factory for this form.
   * When accessing configuration values, use $this->config(). Only use this
   * when the config factory needs to be manipulated directly.
   * @return \Drupal\Core\Config\ConfigFactoryInterface
  protected function configFactory() {
    if (!$this->configFactory) {
      $this->configFactory = $this
    return $this->configFactory;

   * Sets the config factory for this form.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @return $this
  public function setConfigFactory(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    return $this;

   * Resets the configuration factory.
  public function resetConfigFactory() {
    $this->configFactory = NULL;

   * Gets the request object.
   * @return \Symfony\Component\HttpFoundation\Request
   *   The request object.
  protected function getRequest() {
    if (!$this->requestStack) {
      $this->requestStack = \Drupal::service('request_stack');
    return $this->requestStack

   * Gets the route match.
   * @return \Drupal\Core\Routing\RouteMatchInterface
  protected function getRouteMatch() {
    if (!$this->routeMatch) {
      $this->routeMatch = \Drupal::routeMatch();
    return $this->routeMatch;

   * Sets the request stack object to use.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack object.
   * @return $this
  public function setRequestStack(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
    return $this;

   * Gets the current user.
   * @return \Drupal\Core\Session\AccountInterface
   *   The current user.
  protected function currentUser() {
    return \Drupal::currentUser();

   * Returns the service container.
   * This method is marked private to prevent sub-classes from retrieving
   * services from the container through it. Instead,
   * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
   * for injecting services.
   * @return \Symfony\Component\DependencyInjection\ContainerInterface
   *   The service container.
  private function container() {
    return \Drupal::getContainer();

   * Gets the logger for a specific channel.
   * This method exists for backward-compatibility between FormBase and
   * LoggerChannelTrait. Use LoggerChannelTrait::getLogger() instead.
   * @param string $channel
   *   The name of the channel. Can be any string, but the general practice is
   *   to use the name of the subsystem calling this.
   * @return \Psr\Log\LoggerInterface
   *   The logger for the given channel.
  protected function logger($channel) {
    return $this



Contains filters are case sensitive
Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Prevents serializing any properties.
DependencySerializationTrait::__wakeup public function
FormBase::$configFactory protected property The config factory.
FormBase::$requestStack protected property The request stack.
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form.
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm
FormInterface::buildForm public function Form constructor.
FormInterface::getFormId public function Returns a unique string identifying the form.
FormInterface::submitForm public function Form submission handler.
LinkGeneratorTrait::$linkGenerator protected property The link generator.
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger.
MessengerTrait::messenger public function Gets the messenger.
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service.
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator protected function Returns the URL generator service.
UrlGeneratorTrait::redirect protected function Returns a redirect response object for the specified route.
UrlGeneratorTrait::setUrlGenerator public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.