FormBase.php

You are here

Contains \Drupal\Core\Form\FormBase.

Namespace

Drupal\Core\Form

Classes

Namesort descending Description
FormBase Provides a base class for forms.

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\DependencySerialization;
  10. use Drupal\Core\Routing\UrlGeneratorInterface;
  11. use Drupal\Core\StringTranslation\TranslationInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15. * Provides a base class for forms.
  16. */
  17. abstract class FormBase extends DependencySerialization implements FormInterface, ContainerInjectionInterface {
  18. /**
  19. * The translation manager service.
  20. *
  21. * @var \Drupal\Core\StringTranslation\TranslationInterface
  22. */
  23. protected $translationManager;
  24. /**
  25. * The current request.
  26. *
  27. * @var \Symfony\Component\HttpFoundation\Request
  28. */
  29. protected $request;
  30. /**
  31. * The URL generator.
  32. *
  33. * @var \Drupal\Core\Routing\UrlGeneratorInterface
  34. */
  35. protected $urlGenerator;
  36. /**
  37. * The config factory.
  38. *
  39. * @var \Drupal\Core\Config\ConfigFactoryInterface
  40. */
  41. protected $configFactory;
  42. /**
  43. * The form error handler.
  44. *
  45. * @var \Drupal\Core\Form\FormErrorInterface
  46. */
  47. protected $errorHandler;
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public static function create(ContainerInterface $container) {
  52. return new static();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function validateForm(array &$form, array &$form_state) {
  58. // Validation is optional.
  59. }
  60. /**
  61. * Translates a string to the current language or to a given language.
  62. *
  63. * See the t() documentation for details.
  64. */
  65. protected function t($string, array $args = array(), array $options = array()) {
  66. return $this->translationManager()->translate($string, $args, $options);
  67. }
  68. /**
  69. * Generates a URL or path for a specific route based on the given parameters.
  70. *
  71. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  72. * details on the arguments, usage, and possible exceptions.
  73. *
  74. * @return string
  75. * The generated URL for the given route.
  76. */
  77. public function url($route_name, $route_parameters = array(), $options = array()) {
  78. return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  79. }
  80. /**
  81. * Gets the translation manager.
  82. *
  83. * @return \Drupal\Core\StringTranslation\TranslationInterface
  84. * The translation manager.
  85. */
  86. protected function translationManager() {
  87. if (!$this->translationManager) {
  88. $this->translationManager = $this->container()->get('string_translation');
  89. }
  90. return $this->translationManager;
  91. }
  92. /**
  93. * Retrieves a configuration object.
  94. *
  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.
  98. *
  99. * @param string $name
  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.
  104. *
  105. * @return \Drupal\Core\Config\Config
  106. * A configuration object.
  107. */
  108. protected function config($name) {
  109. if (!$this->configFactory) {
  110. $this->configFactory = $this->container()->get('config.factory');
  111. }
  112. return $this->configFactory->get($name);
  113. }
  114. /**
  115. * Sets the translation manager for this form.
  116. *
  117. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
  118. * The translation manager.
  119. *
  120. * @return $this
  121. */
  122. public function setTranslationManager(TranslationInterface $translation_manager) {
  123. $this->translationManager = $translation_manager;
  124. return $this;
  125. }
  126. /**
  127. * Sets the config factory for this form.
  128. *
  129. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  130. * The config factory.
  131. *
  132. * @return $this
  133. */
  134. public function setConfigFactory(ConfigFactoryInterface $config_factory) {
  135. $this->configFactory = $config_factory;
  136. return $this;
  137. }
  138. /**
  139. * Gets the request object.
  140. *
  141. * @return \Symfony\Component\HttpFoundation\Request $request
  142. * The request object.
  143. */
  144. protected function getRequest() {
  145. if (!$this->request) {
  146. $this->request = $this->container()->get('request');
  147. }
  148. return $this->request;
  149. }
  150. /**
  151. * Sets the request object to use.
  152. *
  153. * @param \Symfony\Component\HttpFoundation\Request $request
  154. * The request object.
  155. *
  156. * @return $this
  157. */
  158. public function setRequest(Request $request) {
  159. $this->request = $request;
  160. return $this;
  161. }
  162. /**
  163. * Gets the current user.
  164. *
  165. * @return \Drupal\Core\Session\AccountInterface
  166. * The current user.
  167. */
  168. protected function currentUser() {
  169. return \Drupal::currentUser();
  170. }
  171. /**
  172. * Gets the URL generator.
  173. *
  174. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  175. * The URL generator.
  176. */
  177. protected function urlGenerator() {
  178. if (!$this->urlGenerator) {
  179. $this->urlGenerator = \Drupal::urlGenerator();
  180. }
  181. return $this->urlGenerator;
  182. }
  183. /**
  184. * Sets the URL generator.
  185. *
  186. * @param \Drupal\Core\Routing\UrlGeneratorInterface
  187. * The URL generator.
  188. *
  189. * @return $this
  190. */
  191. public function setUrlGenerator(UrlGeneratorInterface $url_generator) {
  192. $this->urlGenerator = $url_generator;
  193. return $this;
  194. }
  195. /**
  196. * Returns the service container.
  197. *
  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.
  202. *
  203. * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
  204. * The service container.
  205. */
  206. private function container() {
  207. return \Drupal::getContainer();
  208. }
  209. /**
  210. * Returns the form error handler.
  211. *
  212. * @return \Drupal\Core\Form\FormErrorInterface
  213. * The form error handler.
  214. */
  215. protected function errorHandler() {
  216. if (!$this->errorHandler) {
  217. $this->errorHandler = \Drupal::service('form_builder');
  218. }
  219. return $this->errorHandler;
  220. }
  221. /**
  222. * Files an error against a form element.
  223. *
  224. * @param string $name
  225. * The name of the form element.
  226. * @param array $form_state
  227. * An associative array containing the current state of the form.
  228. * @param string $message
  229. * (optional) The error message to present to the user.
  230. *
  231. * @see \Drupal\Core\Form\FormErrorInterface::setErrorByName()
  232. *
  233. * @return $this
  234. */
  235. protected function setFormError($name, array &$form_state, $message = '') {
  236. $this->errorHandler()->setErrorByName($name, $form_state, $message);
  237. return $this;
  238. }
  239. }