Skip to main content
Imagen
Drupal 9 and composer patches
LEVEL
EASY

How to apply patch in Drupal 9 and Drupal 10 with composer

 

If you have come this far, it is because you are faced with the situation of wanting to apply an update or a change that is not currently available in the module.

What is a patch?

A patch is a change in code that primarily fixes, improves, or subtly changes the code of a module.

 

Is it recommended to use a patch?

Patches are temporary changes that we apply to a module, therefore, when the module is updated it is quite likely that this patch will no longer work, either because it has already been integrated into the module or because the files have changed, so is no longer possible to apply it.

 

How do I install patches with composer?

In order to install and manage patches using composer we need to require the "composer-patches" module, we can do it as follows.

composer require cweagans/composer-patches

 

This will update the composer.json file with the indicated module, but we are not finished yet, we will have to create in the "extra" section the place where we will write all our patches.

  "extra": {
    "patches": {
      "drupal/core": {
        "Add startup configuration for PHP server": "https://www.drupal.org/files/issues/add_a_startup-1543858-30.patch"
      }
    }
  }

ATTENTION: This example assumes that you do not have anything else inside extra, which is not common, therefore keep in mind that you have to keep what already exists in that section in case you are using it.

 

In the above example you can see that we are applying a patch to the Drupal core. Now we are going to see an example that will help us understand how to apply a patch.

 

First of all, let's find the patch we need..

Patch that we are going to apply

In this case, as you can see, I want to apply a patch for the "gin" theme. This information is very important in order to apply the patch. In addition I will take the title and ID of the issue to be able to locate this post in the future if I need it, in this case .. "Improve content form detection - 3188521" and finally the patch I want to apply .. in this case "https://www.drupal.org/files/issues/2021-05-19/3188521-8.patch".

 

With all this information we are going to update our composer.json file to apply the patch.

  "extra": {
    "patches": {
      "drupal/core": {
        "Add startup configuration for PHP server": "https://www.drupal.org/files/issues/add_a_startup-1543858-30.patch"
      },
      "drupal/gin": {
        "Improve content form detection - 3188521": "https://www.drupal.org/files/issues/2021-05-19/3188521-8.patch"
      }
    }
  }

 

Once confirmed that the information is correct, we will proceed to execute the following:

composer update

 

This command will remove the module to reinstall it with the patch applied. If everything has gone well, the process will have finished without any errors and we will be able to continue with our project.

 

Patch not applied or throws an error?

It is quite common that throughout our experience we find patches that either do not apply or are no longer compatible.

To see what may be failing we can use the command mentioned above but "verbose" mode which will show us on the screen the reason why the patch cannot be applied

composer update -vvv

There are several answers that it can return, among the most famous, when it cannot find the file or when the lines on which it has to apply the patch cannot be located correctly. So ... is all lost? NO! Here is going to depend on the knowledge and how brave you are, but, we can analyze what the patch does and redo it so that it applies to our version, but this will be for another chapter of Vazcell.

 

And what about you, do you use composer patches in your day-to-day?

 

 

Profile picture for user cms-sys-admin
Adrian Vazquez Peligero
Technical Product Owner and Senior Drupal Developer