abstract class FormBase

You are here

Provides a base class for forms.

Namespace

Drupal\Core\Form

Hierarchy

Expanded class hierarchy of FormBase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DependencySerialization::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerialization::__sleep public function
DependencySerialization::__wakeup public function
FormBase::$configFactory protected property The config factory.
FormBase::$errorHandler protected property The form error handler.
FormBase::$request protected property The current request.
FormBase::$translationManager protected property The translation manager service.
FormBase::$urlGenerator protected property The URL generator.
FormBase::config protected function Retrieves a configuration object.
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this controller. Overrides ContainerInjectionInterface::create
FormBase::currentUser protected function Gets the current user.
FormBase::errorHandler protected function Returns the form error handler.
FormBase::getRequest protected function Gets the request object.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setFormError protected function Files an error against a form element.
FormBase::setRequest public function Sets the request object to use.
FormBase::setTranslationManager public function Sets the translation manager for this form.
FormBase::setUrlGenerator public function Sets the URL generator.
FormBase::t protected function Translates a string to the current language or to a given language.
FormBase::translationManager protected function Gets the translation manager.
FormBase::url public function Generates a URL or path for a specific route based on the given parameters.
FormBase::urlGenerator protected function Gets the URL generator.
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.

File

core/lib/Drupal/Core/Form/FormBase.php, line 21
Contains \Drupal\Core\Form\FormBase.

View source
abstract class FormBase extends DependencySerialization implements FormInterface, ContainerInjectionInterface {

  /**
   * The translation manager service.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationManager;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The form error handler.
   *
   * @var \Drupal\Core\Form\FormErrorInterface
   */
  protected $errorHandler;

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

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {
    // Validation is optional.
  }

  /**
   * Translates a string to the current language or to a given language.
   *
   * See the t() documentation for details.
   */
  protected function t($string, array $args = array(), array $options = array()) {
    return $this->translationManager()->translate($string, $args, $options);
  }

  /**
   * Generates a URL or path for a specific route based on the given parameters.
   *
   * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
   *   details on the arguments, usage, and possible exceptions.
   *
   * @return string
   *   The generated URL for the given route.
   */
  public function url($route_name, $route_parameters = array(), $options = array()) {
    return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  }

  /**
   * Gets the translation manager.
   *
   * @return \Drupal\Core\StringTranslation\TranslationInterface
   *   The translation manager.
   */
  protected function translationManager() {
    if (!$this->translationManager) {
      $this->translationManager = $this->container()->get('string_translation');
    }
    return $this->translationManager;
  }

  /**
   * 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\Config
   *   A configuration object.
   */
  protected function config($name) {
    if (!$this->configFactory) {
      $this->configFactory = $this->container()->get('config.factory');
    }
    return $this->configFactory->get($name);
  }

  /**
   * Sets the translation manager for this form.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
   *   The translation manager.
   *
   * @return $this
   */
  public function setTranslationManager(TranslationInterface $translation_manager) {
    $this->translationManager = $translation_manager;
    return $this;
  }

  /**
   * 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;
  }

  /**
   * Gets the request object.
   *
   * @return \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   */
  protected function getRequest() {
    if (!$this->request) {
      $this->request = $this->container()->get('request');
    }
    return $this->request;
  }

  /**
   * Sets the request object to use.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return $this
   */
  public function setRequest(Request $request) {
    $this->request = $request;
    return $this;
  }

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

  /**
   * Gets the URL generator.
   *
   * @return \Drupal\Core\Routing\UrlGeneratorInterface
   *   The URL generator.
   */
  protected function urlGenerator() {
    if (!$this->urlGenerator) {
      $this->urlGenerator = \Drupal::urlGenerator();
    }
    return $this->urlGenerator;
  }

  /**
   * Sets the URL generator.
   *
   * @param \Drupal\Core\Routing\UrlGeneratorInterface
   *   The URL generator.
   *
   * @return $this
   */
  public function setUrlGenerator(UrlGeneratorInterface $url_generator) {
    $this->urlGenerator = $url_generator;
    return $this;
  }

  /**
   * 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 $container
   *   The service container.
   */
  private function container() {
    return \Drupal::getContainer();
  }

  /**
   * Returns the form error handler.
   *
   * @return \Drupal\Core\Form\FormErrorInterface
   *   The form error handler.
   */
  protected function errorHandler() {
    if (!$this->errorHandler) {
      $this->errorHandler = \Drupal::service('form_builder');
    }
    return $this->errorHandler;
  }

  /**
   * Files an error against a form element.
   *
   * @param string $name
   *   The name of the form element.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   * @param string $message
   *   (optional) The error message to present to the user.
   *
   * @see \Drupal\Core\Form\FormErrorInterface::setErrorByName()
   *
   * @return $this
   */
  protected function setFormError($name, array &$form_state, $message = '') {
    $this->errorHandler()->setErrorByName($name, $form_state, $message);
    return $this;
  }

}
48 files declare their use of FormBase
ActionAdminManageForm.php in core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php
Contains \Drupal\action\Form\ActionAdminManageForm.
AjaxTestDialogForm.php in core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestDialogForm.php
Contains \Drupal\ajax_test\Form\AjaxTestDialogForm.
BanAdmin.php in core/modules/ban/lib/Drupal/ban/Form/BanAdmin.php
Contains \Drupal\ban\Form\BanAdmin.
BookAdminEditForm.php in core/modules/book/lib/Drupal/book/Form/BookAdminEditForm.php
Contains \Drupal\book\Form\BookAdminEditForm.
CommentAdminOverview.php in core/modules/comment/lib/Drupal/comment/Form/CommentAdminOverview.php
Contains \Drupal\comment\Form\CommentAdminOverview.

... See full list