kgaut.net // Développeur web PHP / Drupal freelance
contactez-moi

Drupal, Commande Drush, Injection de dépendances et autowire

Dans le cadre d'un développement custom, j'ai créé pour un fichier de commandes custom dans un module drupal custom, en utilisant la commande

drush generate drush:command-file

j'ai injecté quelques  services, dont un service de mon module courant, en utilisant l'autowiring, (plus besoin de fichier drush.services.yml pour déclarer ses commandes et arguments.

Par contre en lançant ma commande (kmm) pour tester, j'ai le retour suivant : 

$ drush kmm      
Command "kmm" is not defined. 

Moyen clair...

En lançant la commande drush status -vvv je découvre plus d'informations :

 [debug] Could not instantiate Drupal\mon_module\Drush\Commands\ModuleCommands: Cannot autowire service "Drupal\mon_module\ServiceMigrator": argument "$serviceMigrator" of method "Drupal\mon_module\Drush\Commands\ModuleCommands::_construct()", you should configure its value explicitly. [0.49 sec, 10.06 MB]

J'ai donc modifié mon constructeur pour ajouter un attribut spécifiant le service à « autowire » : 

  public function __construct(
    #[Autowire(service: 'mon_module.migrator')] private readonly ServiceMigrator $serviceMigrator,
    #[Autowire(service: 'logger.channel.mon_module_migrator')] private readonly LoggerChannelInterface $loggerChannel,
  ) {
    parent::__construct();
  }

Contenus en rapport

snippet · 8 Fév 2024 Drupal - Créer son premier service

Commentaires (0)