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

Classes

Namesort descending Description
FormBase Provides a base class for forms.