$npx -y skills add edutrul/drupal-ai --skill drupal-servicesDrupal services and dependency injection for controllers, forms, and plugins. Inject services into block plugins, custom plugins, and services using ContainerFactoryPluginInterface and services.yml.
| 1 | # Drupal Services & Dependency Injection |
| 2 | |
| 3 | ## Core Principle |
| 4 | |
| 5 | **Never use `\Drupal::service()` in classes — inject via constructor.** |
| 6 | |
| 7 | ## Dependency Injection Patterns |
| 8 | |
| 9 | ### Controllers & Forms (ContainerInjectionInterface) |
| 10 | |
| 11 | ```php |
| 12 | use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 13 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 14 | |
| 15 | final class MyController implements ContainerInjectionInterface { |
| 16 | public function __construct( |
| 17 | private readonly AccountProxyInterface $currentUser, |
| 18 | private readonly EntityTypeManagerInterface $entityTypeManager, |
| 19 | ) {} |
| 20 | |
| 21 | public static function create(ContainerInterface $container): static { |
| 22 | return new static( |
| 23 | $container->get('current_user'), |
| 24 | $container->get('entity_type.manager'), |
| 25 | ); |
| 26 | } |
| 27 | } |
| 28 | ``` |
| 29 | |
| 30 | ### Plugins (ContainerFactoryPluginInterface) |
| 31 | |
| 32 | ```php |
| 33 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 34 | |
| 35 | final class MyBlock extends BlockBase implements ContainerFactoryPluginInterface { |
| 36 | public function __construct( |
| 37 | array $configuration, |
| 38 | string $plugin_id, |
| 39 | mixed $plugin_definition, |
| 40 | private readonly EntityTypeManagerInterface $entityTypeManager, |
| 41 | ) { |
| 42 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 43 | } |
| 44 | |
| 45 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 46 | return new static( |
| 47 | $configuration, |
| 48 | $plugin_id, |
| 49 | $plugin_definition, |
| 50 | $container->get('entity_type.manager'), |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | ## Service Definition (services.yml) |
| 57 | |
| 58 | ```yaml |
| 59 | services: |
| 60 | my_module.my_service: |
| 61 | class: Drupal\my_module\Service\MyService |
| 62 | arguments: |
| 63 | - '@entity_type.manager' |
| 64 | - '@current_user' |
| 65 | - '@logger.factory' |
| 66 | |
| 67 | # With autowiring (Drupal 10.3+) |
| 68 | Drupal\my_module\Hook\MyModuleHooks: |
| 69 | autowire: true |
| 70 | ``` |
| 71 | |
| 72 | ## Common Service IDs |
| 73 | |
| 74 | | Service ID | Interface | |
| 75 | |---|---| |
| 76 | | `entity_type.manager` | `EntityTypeManagerInterface` | |
| 77 | | `current_user` | `AccountProxyInterface` | |
| 78 | | `logger.factory` | `LoggerChannelFactoryInterface` | |
| 79 | | `database` | `Connection` | |
| 80 | | `config.factory` | `ConfigFactoryInterface` | |
| 81 | | `renderer` | `RendererInterface` | |
| 82 | | `messenger` | `MessengerInterface` | |
| 83 | | `date.formatter` | `DateFormatterInterface` | |
| 84 | | `language_manager` | `LanguageManagerInterface` | |
| 85 | | `path_alias.manager` | `AliasManagerInterface` | |
| 86 | | `module_handler` | `ModuleHandlerInterface` | |
| 87 | | `cache.default` | `CacheBackendInterface` | |
| 88 | |
| 89 | ## StringTranslationTrait |
| 90 | |
| 91 | Add `use StringTranslationTrait;` to classes that need `$this->t()` without full DI of the translation service. |
| 92 | |
| 93 | ## Generating Services with Drush |
| 94 | |
| 95 | ```bash |
| 96 | drush generate service --answers='{ |
| 97 | "module": "my_module", |
| 98 | "service_name": "my_module.helper", |
| 99 | "class": "HelperService", |
| 100 | "services": ["entity_type.manager", "logger.factory"] |
| 101 | }' |
| 102 | ``` |