Applying patch using composer in Drupal

27 Jun 2019
Apply patch using composer in Drupal

In this post, you will understand how to apply the patch using composer to Drupal core and contrib modules/library. In our last post, we explained how to use composer to set up Drupal. We already explained that we need to do the changes in the composer.json file only, not in the composer.lock and with some composer related commands we need to update composer.lock file accordingly.

What are Patches in Drupal

A patch is a structured file that consists of a list of differences between one set of files and another. All code changes, additions, or deletions to Drupal core and contributed modules/themes between developers are done through patches.

Patch using composer

Patches help in development because instead of changing the complete file or module, you only have to fix the part of it to make it work as per your needs. Sometimes these patches are used to fix the core / contrib module related bugs and issues. Later on, these patches can be easily reviewed and incorporated in the modules by the maintainers, this makes the uses of patches more important.

Creating a patch for Drupal modules

First, make sure your issue is not already reported by someone else in the issue queue for the module. Also, check out the latest dev version of the module, is the issue resolved there? If all statements above equal FALSE let's make a patch:

  • Download the module using composer / drush on your local machine.
  • Enter the directory and initialize it with git.
    git init
  • Add all the existing files to git.
    git add *
  • Commit all the added files to the repository.
    git commit -m 'Initial commit'
  • Now, you can make the required changes to make it work as per your requirements.
  • Once you are done with all the changes, you need to check the differences you made to all the files.
    git diff
  • Review the differences carefully and create a patch file.
    git diff > [PatchFileName_IssueNo_CommentNo.patch]

Apply the patch using composer

Now, before you apply the patch using composer, you should ideally create an issue in the module issue queue, if you find it a generic patch, which can be used by others as well [Recommended]. If your patch is kind of specific to your project, place it inside your code repository under patches directory [Less likely to happen].

  • Please edit the composer.json file and add below line and end of the file like below format, for core related changes:
    "extra": {
        "enable-patching": true,
        "patches": {
            "drupal/core": {
                "Issue Details": "Patch file path",
                "Issue Details": "Patch file path"
            }
         }
    }

    for contrib modules related changes:

     "extra": {
        "enable-patching": true,
        "patches": {
            "drupal/[ModuleName]": {
                "Issue Details": "Patch file path",
                "Issue Details": "Patch file path"
            }
         }
    }
  • If a module has more than one patch to be applied, make sure to add a comma at the end of the first patch.
  • There will be no comma at the end of the last patch for the module/library.

Hopefully, this will help you understand how to create and apply the patch using composer and git.