3 namespace Drupal\Core\Controller;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Logger\LoggerChannelTrait;
7 use Drupal\Core\Routing\LinkGeneratorTrait;
8 use Drupal\Core\Routing\RedirectDestinationTrait;
9 use Drupal\Core\Routing\UrlGeneratorTrait;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Utility base class for thin controllers.
16 * Controllers that use this base class have access to a number of utility
17 * methods and to the Container, which can greatly reduce boilerplate dependency
18 * handling code. However, it also makes the class considerably more
19 * difficult to unit test. Therefore this base class should only be used by
20 * controller classes that contain only trivial glue code. Controllers that
21 * contain sufficiently complex logic that it's worth testing should not use
22 * this base class but use ContainerInjectionInterface instead, or even
23 * better be refactored to be trivial glue code.
25 * The services exposed here are those that it is reasonable for a well-behaved
26 * controller to leverage. A controller that needs other services may
27 * need to be refactored into a thin controller and a dependent unit-testable
30 * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
34 abstract class ControllerBase implements ContainerInjectionInterface {
36 use LinkGeneratorTrait;
37 use LoggerChannelTrait;
38 use RedirectDestinationTrait;
39 use StringTranslationTrait;
40 use UrlGeneratorTrait;
45 * @var \Drupal\Core\Entity\EntityManagerInterface
47 protected $entityManager;
50 * The entity type manager.
52 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
54 protected $entityTypeManager;
57 * The entity form builder.
59 * @var \Drupal\Core\Entity\EntityFormBuilderInterface
61 protected $entityFormBuilder;
64 * The language manager.
66 * @var \Drupal\Core\Language\LanguageManagerInterface
68 protected $languageManager;
71 * The configuration factory.
73 * @var \Drupal\Core\Config\ConfigFactoryInterface
75 protected $configFactory;
78 * The key-value storage.
80 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
85 * The current user service.
87 * @var \Drupal\Core\Session\AccountInterface
89 protected $currentUser;
94 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
96 protected $stateService;
101 * @var \Drupal\Core\Extension\ModuleHandlerInterface
103 protected $moduleHandler;
108 * @var \Drupal\Core\Form\FormBuilderInterface
110 protected $formBuilder;
115 public static function create(ContainerInterface $container) {
120 * Retrieves the entity manager service.
122 * @return \Drupal\Core\Entity\EntityManagerInterface
123 * The entity manager service.
125 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
126 * Most of the time static::entityTypeManager() is supposed to be used
129 protected function entityManager() {
130 if (!$this->entityManager) {
131 $this->entityManager = $this->container()->get('entity.manager');
133 return $this->entityManager;
137 * Retrieves the entity type manager.
139 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
140 * The entity type manager.
142 protected function entityTypeManager() {
143 if (!isset($this->entityTypeManager)) {
144 $this->entityTypeManager = $this->container()->get('entity_type.manager');
146 return $this->entityTypeManager;
150 * Retrieves the entity form builder.
152 * @return \Drupal\Core\Entity\EntityFormBuilderInterface
153 * The entity form builder.
155 protected function entityFormBuilder() {
156 if (!$this->entityFormBuilder) {
157 $this->entityFormBuilder = $this->container()->get('entity.form_builder');
159 return $this->entityFormBuilder;
163 * Returns the requested cache bin.
166 * (optional) The cache bin for which the cache object should be returned,
167 * defaults to 'default'.
169 * @return \Drupal\Core\Cache\CacheBackendInterface
170 * The cache object associated with the specified bin.
172 protected function cache($bin = 'default') {
173 return $this->container()->get('cache.' . $bin);
177 * Retrieves a configuration object.
179 * This is the main entry point to the configuration API. Calling
180 * @code $this->config('book.admin') @endcode will return a configuration
181 * object in which the book module can store its administrative settings.
183 * @param string $name
184 * The name of the configuration object to retrieve. The name corresponds to
185 * a configuration file. For @code \Drupal::config('book.admin') @endcode,
186 * the config object returned will contain the contents of book.admin
187 * configuration file.
189 * @return \Drupal\Core\Config\Config
190 * A configuration object.
192 protected function config($name) {
193 if (!$this->configFactory) {
194 $this->configFactory = $this->container()->get('config.factory');
196 return $this->configFactory->get($name);
200 * Returns a key/value storage collection.
202 * @param string $collection
203 * Name of the key/value collection to return.
205 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
207 protected function keyValue($collection) {
208 if (!$this->keyValue) {
209 $this->keyValue = $this->container()->get('keyvalue')->get($collection);
211 return $this->keyValue;
215 * Returns the state storage service.
217 * Use this to store machine-generated data, local to a specific environment
218 * that does not need deploying and does not need human editing; for example,
219 * the last time cron was run. Data which needs to be edited by humans and
220 * needs to be the same across development, production, etc. environments
221 * (for example, the system maintenance message) should use config() instead.
223 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
225 protected function state() {
226 if (!$this->stateService) {
227 $this->stateService = $this->container()->get('state');
229 return $this->stateService;
233 * Returns the module handler.
235 * @return \Drupal\Core\Extension\ModuleHandlerInterface
237 protected function moduleHandler() {
238 if (!$this->moduleHandler) {
239 $this->moduleHandler = $this->container()->get('module_handler');
241 return $this->moduleHandler;
245 * Returns the form builder service.
247 * @return \Drupal\Core\Form\FormBuilderInterface
249 protected function formBuilder() {
250 if (!$this->formBuilder) {
251 $this->formBuilder = $this->container()->get('form_builder');
253 return $this->formBuilder;
257 * Returns the current user.
259 * @return \Drupal\Core\Session\AccountInterface
262 protected function currentUser() {
263 if (!$this->currentUser) {
264 $this->currentUser = $this->container()->get('current_user');
266 return $this->currentUser;
270 * Returns the language manager service.
272 * @return \Drupal\Core\Language\LanguageManagerInterface
273 * The language manager.
275 protected function languageManager() {
276 if (!$this->languageManager) {
277 $this->languageManager = $this->container()->get('language_manager');
279 return $this->languageManager;
283 * Returns the service container.
285 * This method is marked private to prevent sub-classes from retrieving
286 * services from the container through it. Instead,
287 * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
288 * for injecting services.
290 * @return \Symfony\Component\DependencyInjection\ContainerInterface
291 * The service container.
293 private function container() {
294 return \Drupal::getContainer();