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.