Patching your Drupal site : Automate or Do It Yourself .

Drupal is a powerful and flexible content management system that relies on its community for continuous improvement. As part of this collaborative effort, contributors often submit patches to fix bugs, add features, or enhance security. In this blog post, we'll explore the process of applying a patch to your Drupal site.There are several reasons why we would want to apply a patch in a Drupal project, but the two most common ones are :

  1. Modify modules without hacking files: Avoids losing your changes when the module updates.
  2. Improve or fix modules: Implement solutions not yet included by the module author.
How to apply Patch through Composer ?

The first important thing is to know that any change to someone else's code that we are not fully familiar with can lead to problems. So we have to be sure of what we are doing, then we can proceed with patching the module. The process involves using Composer to apply the patch. It is also necessary to install the following package if your project doesn't already contain it:

composer require cweagans/composer-patches

Once we install the package, all that remains is to add the appropriate configuration to the extra section of the main composer.json file of your project.

...
"require": {...},
"require-dev": {...},
"extra": {
   "patches": {
       "drupal/field_inheritance": {
           "Issue fix menu link": "https://www.drupal.org/files/issues/2022-12-24/field_inheritance.1.x-dev.rector.patch"
       }
   },
   "drupal-scaffold": {...},
   "installer-paths": {...}
}
... 

Explanation -

The patch will be applied after running the Composer require install, or update commands. Eg composer update drupal/field_inheritance command.

Note - Usually, a patch is created only for a specific version of a module. This means the same patch will likely not work for a new version. And that can be a problem. Therefore, one solution is to lock the module version in the composer.json file. That way, we are sure that we will always have a properly patched module.

But that obviously can lead to outdated modules. If you choose not to lock a version, then you have to take extra care during updates to make sure that all patches are applied. If some patches fail, you will have to find a new version of the patch or do something else like maybe create a new version of the patch yourself.

Just to make sure that you know how to lock a version, this is the unlocked version:

"drupal/field_inheritance": "^2.0.0"

and this is a module locked to a specific version:

"drupal/field_inheritance": "2.0.0"

This refers to the version constraint in the composer.json file.

In case Composer cannot apply the patch successfully, you will receive an error that looks something like this:

Could not apply patch! Skipping. The error was: Cannot apply patch https:...

How to apply Patch manually ?

You can also apply the patch using Git. Clone the module, specifically the version for which the patch was created, then download the patch file, place it in the module directory, and run the following command in the terminal:

git apply -v PATCH_NAME.patch


 

Share on social media

Add new comment