Drush – the essential Drupal command line tool

Drush is a command line tool for Drupal that allows developers to both undertake routine tasks from the cli as well as create their own tasks or manage more complex processes.

Using Drush is something of a no-brainer for developers and you can make as much or as little of it as you want.  We’ll be coming back to Drush time and again in this blog, but for now here’s how to get started and some useful commands to run.

Installing Drush

Drush is installed via Composer when you setup your Drupal 10 site.  You don’t need to install anything yourself.  However, you may need to specify the version of PHP specifically for Drush, depending on your setup.  You can do this in your bash profile, like this (in this example we’re setting Drush to run PHP8.1):

export DRUSH_PHP=/Applications/MAMP/bin/php/php8.1.13/bin/php

Running Drush

Head to your Drupal 10 root in Terminal and run drush.  You'll see a long list of commands you can use, if everything works.  You may find that Drush is not accessible globally and you need to be more specific.  In which case you can run vendor/bin/drush to directly run drush as installed on your Drupal 10 site.

Useful commands

  • drush cr clear the Drupal cache
  • drush pm:install <module_name> install a Drupal module
  • drush uli generate a one-time link to login to the CMS as admin user
  • drush sql-dump --gzip --result-file="/@DATABASE_@DATE.sql" dump the database as gzip file with database name and date as the filename
  • drush cex export the configs to the sync directory

Manually applying a Drupal module patch

Drupal module patches go through a thorough testing process before being made available as a version update for the module.  This is generally an excellent workflow, but sometimes you’ll want to apply a community patch before it’s merged into the module.

The finding problem

You’ve found an bug with a Drupal contrib module.  Your first step should be to look for an issue matching this problem in the module’s issue queue, in case a patch has been proposed.  To find the issue queue, head to the official Drupal page for the module and use the Issue search on the right hand side.  If you’ve found your problem here, read through the thread and look for a proposed patch.

The patch

With any luck, you’ll not only find your issue but also a proposed .patch file.  This will often be accompanied by a a status, such as Needs Review.  Download the patch file and save it to your_site/patches/patch_name.patch

Crucially, you must now review the code in the patch and understand what it’s doing to the affected module.  The hard work of testing and merging this patch has not been completed, so you need to do it yourself.  The patch could introduce security vulnerabilities or other bugs, or just not work very well.

Adding the patch

This needs to be done via Composer.  Open composer.json and update the json within “extra” to identify your patch:


{
    "extra": {
        "patches": {
             "drupal/core": {
                 "Description of what my_patch.patch does": "patches/my_patch.patch"
              }
        }
    }
}

Applying the patch

Run composer install from your root to apply the patch.

Test the patch

Review the module code to confirm what's bee changed and, crucuially, again satisfy yourself that it's not made things worse or no better.  You should now test the site to check it's solved the real problem.

Maintaining the patch

When the module is updated, there's a good chance the patch will break.  This is a good thing, as it will prompt you to check if the patch has been merged into the module or not. If it has, you can remove it.  If it has not, then you can create a new patch or check the issue queue for updates.