Module Development

Create a Custom Drush Code Generator for Drupal 8, 9, and 10

In addition to using one of the existing generators, developers can write their own Drush generator commands. This can help speed up repetitive tasks and reduce the use of boilerplate code that is prone to human error.

Generators are provided through Drush's integration with the Drupal Code Generator project. Writing new generators isn't specific to Drush, though if you're creating generators for Drupal it is definitely easiest with Drush as a wrapper.

Similar to Drush commands, generators can be supplied by a Drupal module or declared globally. If you have a feature-specific functionality, it's best to ship your custom generator within the custom module. Otherwise, a global generator can be declared and used.

In this tutorial we'll:

  • Explain the anatomy of a Drush generator
  • Write a custom Drush generator for handling a site's file, and use it in a project

By the end of this tutorial you should understand how to create, or customize, a Drush code generator and use it in your project.