Contains \Drupal\Core\Form\FormBase.

Namespace

Drupal\Core\Form

File

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

Classes

Namesort descending Description
FormBase Provides a base class for forms.