3 namespace Drupal\Core\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
8 use Drupal\Core\Logger\LoggerChannelTrait;
9 use Drupal\Core\Routing\LinkGeneratorTrait;
10 use Drupal\Core\Routing\RedirectDestinationTrait;
11 use Drupal\Core\Routing\UrlGeneratorTrait;
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\HttpFoundation\RequestStack;
17 * Provides a base class for forms.
19 * This class exists as a mid-point between dependency injection through
20 * ContainerInjectionInterface, and a less-structured use of traits which
21 * default to using the \Drupal accessor for service discovery.
23 * To properly inject services, override create() and use the setters provided
24 * by the traits to inject the needed services.
27 * public static function create($container) {
28 * $form = new static();
29 * // In this example we only need string translation so we use the
30 * // setStringTranslation() method provided by StringTranslationTrait.
31 * $form->setStringTranslation($container->get('string_translation'));
36 * Alternately, do not use FormBase. A class can implement FormInterface, use
37 * the traits it needs, and inject services from the container as required.
41 * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
43 abstract class FormBase implements FormInterface, ContainerInjectionInterface {
45 use DependencySerializationTrait;
46 use LinkGeneratorTrait;
47 use LoggerChannelTrait;
48 use RedirectDestinationTrait;
49 use StringTranslationTrait;
50 use UrlGeneratorTrait;
55 * @var \Symfony\Component\HttpFoundation\RequestStack
57 protected $requestStack;
62 * Subclasses should use the self::config() method, which may be overridden to
63 * address specific needs when loading config, rather than this property
64 * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
67 * @var \Drupal\Core\Config\ConfigFactoryInterface
69 protected $configFactory;
74 * @var \Drupal\Core\Routing\RouteMatchInterface
76 protected $routeMatch;
81 public static function create(ContainerInterface $container) {
88 public function validateForm(array &$form, FormStateInterface $form_state) {
89 // Validation is optional.
93 * Retrieves a configuration object.
95 * This is the main entry point to the configuration API. Calling
96 * @code $this->config('book.admin') @endcode will return a configuration
97 * object in which the book module can store its administrative settings.
100 * The name of the configuration object to retrieve. The name corresponds to
101 * a configuration file. For @code \Drupal::config('book.admin') @endcode,
102 * the config object returned will contain the contents of book.admin
103 * configuration file.
105 * @return \Drupal\Core\Config\ImmutableConfig
106 * A configuration object.
108 protected function config($name) {
109 return $this->configFactory()->get($name);
113 * Gets the config factory for this form.
115 * When accessing configuration values, use $this->config(). Only use this
116 * when the config factory needs to be manipulated directly.
118 * @return \Drupal\Core\Config\ConfigFactoryInterface
120 protected function configFactory() {
121 if (!$this->configFactory) {
122 $this->configFactory = $this->container()->get('config.factory');
124 return $this->configFactory;
128 * Sets the config factory for this form.
130 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
131 * The config factory.
135 public function setConfigFactory(ConfigFactoryInterface $config_factory) {
136 $this->configFactory = $config_factory;
141 * Resets the configuration factory.
143 public function resetConfigFactory() {
144 $this->configFactory = NULL;
148 * Gets the request object.
150 * @return \Symfony\Component\HttpFoundation\Request
151 * The request object.
153 protected function getRequest() {
154 if (!$this->requestStack) {
155 $this->requestStack = \Drupal::service('request_stack');
157 return $this->requestStack->getCurrentRequest();
161 * Gets the route match.
163 * @return \Drupal\Core\Routing\RouteMatchInterface
165 protected function getRouteMatch() {
166 if (!$this->routeMatch) {
167 $this->routeMatch = \Drupal::routeMatch();
169 return $this->routeMatch;
173 * Sets the request stack object to use.
175 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
176 * The request stack object.
180 public function setRequestStack(RequestStack $request_stack) {
181 $this->requestStack = $request_stack;
186 * Gets the current user.
188 * @return \Drupal\Core\Session\AccountInterface
191 protected function currentUser() {
192 return \Drupal::currentUser();
196 * Returns the service container.
198 * This method is marked private to prevent sub-classes from retrieving
199 * services from the container through it. Instead,
200 * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
201 * for injecting services.
203 * @return \Symfony\Component\DependencyInjection\ContainerInterface
204 * The service container.
206 private function container() {
207 return \Drupal::getContainer();
211 * Gets the logger for a specific channel.
213 * This method exists for backward-compatibility between FormBase and
214 * LoggerChannelTrait. Use LoggerChannelTrait::getLogger() instead.
216 * @param string $channel
217 * The name of the channel. Can be any string, but the general practice is
218 * to use the name of the subsystem calling this.
220 * @return \Psr\Log\LoggerInterface
221 * The logger for the given channel.
223 protected function logger($channel) {
224 return $this->getLogger($channel);