Skip to content

Drupal Services & Dependency Injection

Drupal 11.x (applicable to Drupal 10.x+) | Philosophy: Services ARE configuration — defined in YAML, managed by the container

I need to...

Task Guide
Understand what services and DI are Services & DI Overview
Learn how the service container works Service Container Architecture
Understand the services.yml file structure services.yml Schema
Know all available service definition properties Service Definition Properties
Define a new service Defining Services
Use autowiring for my service Autowiring
Inject services into a controller or form Constructor Injection
Inject services into a plugin (block, field formatter) Plugin Injection
Decide when to use \Drupal vs dependency injection The \Drupal Global Helper
Tag a service (event_subscriber, cache.context, etc.) Service Tags
Collect multiple tagged services Tagged Service Collectors
Create an event subscriber Event Subscribers
Alter existing services Service Providers & Altering
Add a compiler pass Compiler Passes
Find what core services are available Core Services Reference
Use a factory to create services Factory Services
Handle service serialization DependencySerializationTrait
Learn DI best practices Best Practices & Patterns
Avoid common mistakes Anti-Patterns & Common Mistakes
Understand security and performance Security & Performance
Find code references Code Reference Map