entityManager) { $this->entityManager = $this->container()->get('entity.manager'); } return $this->entityManager; } /** * Retrieves the entity type manager. * * @return \Drupal\Core\Entity\EntityTypeManagerInterface * The entity type manager. */ protected function entityTypeManager() { if (!isset($this->entityTypeManager)) { $this->entityTypeManager = $this->container()->get('entity_type.manager'); } return $this->entityTypeManager; } /** * Retrieves the entity form builder. * * @return \Drupal\Core\Entity\EntityFormBuilderInterface * The entity form builder. */ protected function entityFormBuilder() { if (!$this->entityFormBuilder) { $this->entityFormBuilder = $this->container()->get('entity.form_builder'); } return $this->entityFormBuilder; } /** * Returns the requested cache bin. * * @param string $bin * (optional) The cache bin for which the cache object should be returned, * defaults to 'default'. * * @return \Drupal\Core\Cache\CacheBackendInterface * The cache object associated with the specified bin. */ protected function cache($bin = 'default') { return $this->container()->get('cache.' . $bin); } /** * Retrieves a configuration object. * * This is the main entry point to the configuration API. Calling * @code $this->config('book.admin') @endcode will return a configuration * object in which the book module can store its administrative settings. * * @param string $name * The name of the configuration object to retrieve. The name corresponds to * a configuration file. For @code \Drupal::config('book.admin') @endcode, * the config object returned will contain the contents of book.admin * configuration file. * * @return \Drupal\Core\Config\Config * A configuration object. */ protected function config($name) { if (!$this->configFactory) { $this->configFactory = $this->container()->get('config.factory'); } return $this->configFactory->get($name); } /** * Returns a key/value storage collection. * * @param string $collection * Name of the key/value collection to return. * * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function keyValue($collection) { if (!$this->keyValue) { $this->keyValue = $this->container()->get('keyvalue')->get($collection); } return $this->keyValue; } /** * Returns the state storage service. * * Use this to store machine-generated data, local to a specific environment * that does not need deploying and does not need human editing; for example, * the last time cron was run. Data which needs to be edited by humans and * needs to be the same across development, production, etc. environments * (for example, the system maintenance message) should use config() instead. * * @return \Drupal\Core\State\StateInterface */ protected function state() { if (!$this->stateService) { $this->stateService = $this->container()->get('state'); } return $this->stateService; } /** * Returns the module handler. * * @return \Drupal\Core\Extension\ModuleHandlerInterface */ protected function moduleHandler() { if (!$this->moduleHandler) { $this->moduleHandler = $this->container()->get('module_handler'); } return $this->moduleHandler; } /** * Returns the form builder service. * * @return \Drupal\Core\Form\FormBuilderInterface */ protected function formBuilder() { if (!$this->formBuilder) { $this->formBuilder = $this->container()->get('form_builder'); } return $this->formBuilder; } /** * Returns the current user. * * @return \Drupal\Core\Session\AccountInterface * The current user. */ protected function currentUser() { if (!$this->currentUser) { $this->currentUser = $this->container()->get('current_user'); } return $this->currentUser; } /** * Returns the language manager service. * * @return \Drupal\Core\Language\LanguageManagerInterface * The language manager. */ protected function languageManager() { if (!$this->languageManager) { $this->languageManager = $this->container()->get('language_manager'); } return $this->languageManager; } /** * Returns the service container. * * This method is marked private to prevent sub-classes from retrieving * services from the container through it. Instead, * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used * for injecting services. * * @return \Symfony\Component\DependencyInjection\ContainerInterface * The service container. */ private function container() { return \Drupal::getContainer(); } }