1. 8.2.x core/lib/Drupal/Core/Form/FormBase.php
  2. 8.0.x core/lib/Drupal/Core/Form/FormBase.php
  3. 8.1.x core/lib/Drupal/Core/Form/FormBase.php

Namespace

Drupal\Core\Form

File

core/lib/Drupal/Core/Form/FormBase.php
View source
  1. <?php
  2. namespace Drupal\Core\Form;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  5. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  6. use Drupal\Core\Logger\LoggerChannelTrait;
  7. use Drupal\Core\Routing\LinkGeneratorTrait;
  8. use Drupal\Core\Routing\RedirectDestinationTrait;
  9. use Drupal\Core\Routing\UrlGeneratorTrait;
  10. use Drupal\Core\StringTranslation\StringTranslationTrait;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. /**
  14. * Provides a base class for forms.
  15. *
  16. * This class exists as a mid-point between dependency injection through
  17. * ContainerInjectionInterface, and a less-structured use of traits which
  18. * default to using the \Drupal accessor for service discovery.
  19. *
  20. * To properly inject services, override create() and use the setters provided
  21. * by the traits to inject the needed services.
  22. *
  23. * @code
  24. * public static function create($container) {
  25. * $form = new static();
  26. * // In this example we only need string translation so we use the
  27. * // setStringTranslation() method provided by StringTranslationTrait.
  28. * $form->setStringTranslation($container->get('string_translation'));
  29. * return $form;
  30. * }
  31. * @endcode
  32. *
  33. * Alternately, do not use FormBase. A class can implement FormInterface, use
  34. * the traits it needs, and inject services from the container as required.
  35. *
  36. * @ingroup form_api
  37. *
  38. * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
  39. */
  40. abstract class FormBase implements FormInterface, ContainerInjectionInterface {
  41. use DependencySerializationTrait;
  42. use LinkGeneratorTrait;
  43. use LoggerChannelTrait;
  44. use RedirectDestinationTrait;
  45. use StringTranslationTrait;
  46. use UrlGeneratorTrait;
  47. /**
  48. * The request stack.
  49. *
  50. * @var \Symfony\Component\HttpFoundation\RequestStack
  51. */
  52. protected $requestStack;
  53. /**
  54. * The config factory.
  55. *
  56. * Subclasses should use the self::config() method, which may be overridden to
  57. * address specific needs when loading config, rather than this property
  58. * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
  59. * this.
  60. *
  61. * @var \Drupal\Core\Config\ConfigFactoryInterface
  62. */
  63. protected $configFactory;
  64. /**
  65. * The route match.
  66. *
  67. * @var \Drupal\Core\Routing\RouteMatchInterface
  68. */
  69. protected $routeMatch;
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public static function create(ContainerInterface $container) {
  74. return new static();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function validateForm(array &$form, FormStateInterface $form_state) {
  80. // Validation is optional.
  81. }
  82. /**
  83. * Retrieves a configuration object.
  84. *
  85. * This is the main entry point to the configuration API. Calling
  86. * @code $this->config('book.admin') @endcode will return a configuration
  87. * object in which the book module can store its administrative settings.
  88. *
  89. * @param string $name
  90. * The name of the configuration object to retrieve. The name corresponds to
  91. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  92. * the config object returned will contain the contents of book.admin
  93. * configuration file.
  94. *
  95. * @return \Drupal\Core\Config\ImmutableConfig
  96. * A configuration object.
  97. */
  98. protected function config($name) {
  99. return $this->configFactory()->get($name);
  100. }
  101. /**
  102. * Gets the config factory for this form.
  103. *
  104. * When accessing configuration values, use $this->config(). Only use this
  105. * when the config factory needs to be manipulated directly.
  106. *
  107. * @return \Drupal\Core\Config\ConfigFactoryInterface
  108. */
  109. protected function configFactory() {
  110. if (!$this->configFactory) {
  111. $this->configFactory = $this->container()->get('config.factory');
  112. }
  113. return $this->configFactory;
  114. }
  115. /**
  116. * Sets the config factory for this form.
  117. *
  118. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  119. * The config factory.
  120. *
  121. * @return $this
  122. */
  123. public function setConfigFactory(ConfigFactoryInterface $config_factory) {
  124. $this->configFactory = $config_factory;
  125. return $this;
  126. }
  127. /**
  128. * Resets the configuration factory.
  129. */
  130. public function resetConfigFactory() {
  131. $this->configFactory = NULL;
  132. }
  133. /**
  134. * Gets the request object.
  135. *
  136. * @return \Symfony\Component\HttpFoundation\Request $request
  137. * The request object.
  138. */
  139. protected function getRequest() {
  140. if (!$this->requestStack) {
  141. $this->requestStack = \Drupal::service('request_stack');
  142. }
  143. return $this->requestStack->getCurrentRequest();
  144. }
  145. /**
  146. * Gets the route match.
  147. *
  148. * @return \Drupal\Core\Routing\RouteMatchInterface
  149. */
  150. protected function getRouteMatch() {
  151. if (!$this->routeMatch) {
  152. $this->routeMatch = \Drupal::routeMatch();
  153. }
  154. return $this->routeMatch;
  155. }
  156. /**
  157. * Sets the request stack object to use.
  158. *
  159. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  160. * The request stack object.
  161. *
  162. * @return $this
  163. */
  164. public function setRequestStack(RequestStack $request_stack) {
  165. $this->requestStack = $request_stack;
  166. return $this;
  167. }
  168. /**
  169. * Gets the current user.
  170. *
  171. * @return \Drupal\Core\Session\AccountInterface
  172. * The current user.
  173. */
  174. protected function currentUser() {
  175. return \Drupal::currentUser();
  176. }
  177. /**
  178. * Returns the service container.
  179. *
  180. * This method is marked private to prevent sub-classes from retrieving
  181. * services from the container through it. Instead,
  182. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
  183. * for injecting services.
  184. *
  185. * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
  186. * The service container.
  187. */
  188. private function container() {
  189. return \Drupal::getContainer();
  190. }
  191. /**
  192. * Gets the logger for a specific channel.
  193. *
  194. * This method exists for backward-compatibility between FormBase and
  195. * LoggerChannelTrait. Use LoggerChannelTrait::getLogger() instead.
  196. *
  197. * @param string $channel
  198. * The name of the channel. Can be any string, but the general practice is
  199. * to use the name of the subsystem calling this.
  200. *
  201. * @return \Psr\Log\LoggerInterface
  202. * The logger for the given channel.
  203. */
  204. protected function logger($channel) {
  205. return $this->getLogger($channel);
  206. }
  207. }

Classes

Namesort descending Description
FormBase Provides a base class for forms.