Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\Core\Form\FormBase.
*/
namespace Drupal\Core\Form;
Alex Pott
committed
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Dries Buytaert
committed
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\UrlGeneratorTrait;
Alex Pott
committed
use Drupal\Core\StringTranslation\StringTranslationTrait;
Alex Pott
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Alex Pott
committed
/**
* Provides a base class for forms.
*/
Dries Buytaert
committed
abstract class FormBase implements FormInterface, ContainerInjectionInterface {
Alex Pott
committed
use StringTranslationTrait;
Dries Buytaert
committed
use DependencySerializationTrait;
use LinkGeneratorTrait;
use UrlGeneratorTrait;
Alex Pott
committed
/**
* The request stack.
Alex Pott
committed
*
* @var \Symfony\Component\HttpFoundation\RequestStack
Alex Pott
committed
*/
protected $requestStack;
Alex Pott
committed
Angie Byron
committed
/**
* The config factory.
*
Dries Buytaert
committed
* 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.
Alex Pott
committed
*
Alex Pott
committed
* @var \Drupal\Core\Config\ConfigFactoryInterface
Angie Byron
committed
*/
Dries Buytaert
committed
protected $configFactory;
Angie Byron
committed
/**
* The form error handler.
*
* @var \Drupal\Core\Form\FormErrorInterface
*/
protected $errorHandler;
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
// Validation is optional.
}
Angie Byron
committed
/**
* 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) {
Alex Pott
committed
return $this->configFactory()->get($name);
}
/**
* 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() {
Angie Byron
committed
if (!$this->configFactory) {
$this->configFactory = $this->container()->get('config.factory');
}
Alex Pott
committed
return $this->configFactory;
Angie Byron
committed
}
/**
* Sets the config factory for this form.
*
Alex Pott
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Angie Byron
committed
* The config factory.
*
* @return $this
Angie Byron
committed
*/
Alex Pott
committed
public function setConfigFactory(ConfigFactoryInterface $config_factory) {
Angie Byron
committed
$this->configFactory = $config_factory;
return $this;
}
Alex Pott
committed
/**
* Resets the configuration factory.
*/
public function resetConfigFactory() {
$this->configFactory = NULL;
}
Alex Pott
committed
/**
* Gets the request object.
*
* @return \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
protected function getRequest() {
if (!$this->requestStack) {
$this->requestStack = \Drupal::service('request_stack');
Alex Pott
committed
}
return $this->requestStack->getCurrentRequest();
Alex Pott
committed
}
/**
* Sets the request stack object to use.
Alex Pott
committed
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack object.
*
* @return $this
Alex Pott
committed
*/
public function setRequestStack(RequestStack $request_stack) {
$this->requestStack = $request_stack;
return $this;
Alex Pott
committed
}
/**
* Gets the current user.
*
* @return \Drupal\Core\Session\AccountInterface
* The current user.
*/
protected function currentUser() {
return \Drupal::currentUser();
Alex Pott
committed
}
/**
* 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;
}