Code generation using drupal console

Drupal console, like symfony console, provides dozens of commands for generating modules and boilerplate code in the Drupal ecosystem, including the following.:

Generate a module

1
drupal gm --module helloworld --dependencies jsonapi

If you don’t already have a directory, this command will create one for you.

Generate services

1
drupal generate:service --module=helloworld --path-service="/modules/custom/helloworld/src/" --name="helloworld.painter" --class="Painter"

Generate a controller

1
drupal generate:controller --module=helloworld --class="DefaultController"

If your service depends on another services when you prompt with Enter your service []: enter the name of the dependencies services and press enter for example helloworld.fancy_logger, that will do a couple of things:

  1. Add the dependency in the argument [] section of the service declaration in the service.yaml.
  2. And injects the said service class/ interface in the constructor.
updatedupdated2024-01-172024-01-17