Skip to content

ReferenceFor module developersDrupal 11

Common services

Drupal’s behavior lives in services resolved from the container. In a class that supports autowiring, type-hint the fully qualified name in the table and Drupal injects the service. Autowiring works only when core defines an alias for that type; every type below has one except the cache bin, which needs an explicit service ID. The service ID is what you name in a *.services.yml arguments list or pass to \Drupal::service(). This is the shortlist you reach for daily. For the full container, see Services and dependency injection.

Constructor injection with autowiring:

public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly AccountProxyInterface $currentUser,
) {}
Service IDType-hint
entity_type.manager\Drupal\Core\Entity\EntityTypeManagerInterface
entity.repository\Drupal\Core\Entity\EntityRepositoryInterface
entity_field.manager\Drupal\Core\Entity\EntityFieldManagerInterface
entity_type.bundle.info\Drupal\Core\Entity\EntityTypeBundleInfoInterface
Service IDType-hint
current_route_match\Drupal\Core\Routing\RouteMatchInterface
request_stack\Symfony\Component\HttpFoundation\RequestStack
current_user\Drupal\Core\Session\AccountProxyInterface
path_alias.manager\Drupal\path_alias\AliasManagerInterface
url_generator\Drupal\Core\Routing\UrlGeneratorInterface
Service IDType-hintUse for
database\Drupal\Core\Database\ConnectionDirect SQL when the entity API does not fit.
config.factory\Drupal\Core\Config\ConfigFactoryInterfaceSettings that ship as configuration.
state\Drupal\Core\State\StateInterfaceSmall, environment-specific values that are not configuration.
keyvalue\Drupal\Core\KeyValueStore\KeyValueFactoryInterfaceArbitrary keyed collections.
tempstore.private\Drupal\Core\TempStore\PrivateTempStoreFactoryPer-user work in progress (wizards, carts).
Service IDType-hint
renderer\Drupal\Core\Render\RendererInterface
messenger\Drupal\Core\Messenger\MessengerInterface
string_translation\Drupal\Core\StringTranslation\TranslationInterface
date.formatter\Drupal\Core\Datetime\DateFormatterInterface
Service IDType-hintUse for
datetime.time\Drupal\Component\Datetime\TimeInterfaceThe request timestamp (testable, unlike time()).
logger.factory\Drupal\Core\Logger\LoggerChannelFactoryInterfaceGet a named logger channel.
cache.default\Drupal\Core\Cache\CacheBackendInterfaceA general-purpose cache bin. No alias exists, because every bin shares this interface: add Symfony’s #[Autowire(service: 'cache.default')] to the parameter, or list '@cache.default' in arguments. The same applies to every cache.* bin.
cache_tags.invalidator\Drupal\Core\Cache\CacheTagsInvalidatorInterfaceInvalidate cached output by tag.
http_client\GuzzleHttp\ClientInterfaceOutbound HTTP requests.
queue\Drupal\Core\Queue\QueueFactoryBackground work.
file_system\Drupal\Core\File\FileSystemInterfaceFile operations through stream wrappers.
file_url_generator\Drupal\Core\File\FileUrlGeneratorInterfacePublic and private file URLs.
module_handler\Drupal\Core\Extension\ModuleHandlerInterfaceCheck installed modules; invoke hooks.
event_dispatcher\Symfony\Contracts\EventDispatcher\EventDispatcherInterfaceDispatch and subscribe to events.
language_manager\Drupal\Core\Language\LanguageManagerInterfaceLanguages and the current language.

Was this page helpful?

Drupal is a registered trademark of Dries Buytaert.