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