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