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.
Example
Section titled “Example”Constructor injection with autowiring:
public function __construct( private readonly EntityTypeManagerInterface $entityTypeManager, private readonly AccountProxyInterface $currentUser,) {}Entities
Section titled “Entities”| Service ID | Type-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 |
Request, routing, and the current user
Section titled “Request, routing, and the current user”| Service ID | Type-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 |
Storage
Section titled “Storage”| Service ID | Type-hint | Use for |
|---|---|---|
database | \Drupal\Core\Database\Connection | Direct SQL when the entity API does not fit. |
config.factory | \Drupal\Core\Config\ConfigFactoryInterface | Settings that ship as configuration. |
state | \Drupal\Core\State\StateInterface | Small, environment-specific values that are not configuration. |
keyvalue | \Drupal\Core\KeyValueStore\KeyValueFactoryInterface | Arbitrary keyed collections. |
tempstore.private | \Drupal\Core\TempStore\PrivateTempStoreFactory | Per-user work in progress (wizards, carts). |
Output and messaging
Section titled “Output and messaging”| Service ID | Type-hint |
|---|---|
renderer | \Drupal\Core\Render\RendererInterface |
messenger | \Drupal\Core\Messenger\MessengerInterface |
string_translation | \Drupal\Core\StringTranslation\TranslationInterface |
date.formatter | \Drupal\Core\Datetime\DateFormatterInterface |
Infrastructure
Section titled “Infrastructure”| Service ID | Type-hint | Use for |
|---|---|---|
datetime.time | \Drupal\Component\Datetime\TimeInterface | The request timestamp (testable, unlike time()). |
logger.factory | \Drupal\Core\Logger\LoggerChannelFactoryInterface | Get a named logger channel. |
cache.default | \Drupal\Core\Cache\CacheBackendInterface | A 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\CacheTagsInvalidatorInterface | Invalidate cached output by tag. |
http_client | \GuzzleHttp\ClientInterface | Outbound HTTP requests. |
queue | \Drupal\Core\Queue\QueueFactory | Background work. |
file_system | \Drupal\Core\File\FileSystemInterface | File operations through stream wrappers. |
file_url_generator | \Drupal\Core\File\FileUrlGeneratorInterface | Public and private file URLs. |
module_handler | \Drupal\Core\Extension\ModuleHandlerInterface | Check installed modules; invoke hooks. |
event_dispatcher | \Symfony\Contracts\EventDispatcher\EventDispatcherInterface | Dispatch and subscribe to events. |
language_manager | \Drupal\Core\Language\LanguageManagerInterface | Languages and the current language. |
Related
Section titled “Related”- Services and dependency injection explains autowiring, factories, and tagged services.
- Services and events shows defining your own service.
- Browse every service on the
services and the container topicon api.drupal.org.
Was this page helpful?
Drupal is a registered trademark of Dries Buytaert.