To how apply patch file for composer based Drupal 9

25th May 2022
Table of contents

At some point in time when you have been working with Drupal 8/9 for awhile, you may have to learn how to apply patch to modules that have some issues or perhaps to test a functionality that you needed for your newly build Drupal site.

The procedure is to apply a patch file for composer based Drupal 8/9 website is as follows.

Look in the module's issue queues page, this is where patches are submitted to for testing and fixing bugs.

To begin, we need to add "cweagans/composer-patches" plugin to aid us in patching modules. We can add this to Composer by typing in command line:

composer require cweagans/composer-patches

Applying a patch with Composer

Edit the composer.json file and add below these lines to the "extra": object:

"extra": {
    "enable-patching": true,
    "patches": {
        "drupal/core": {
            "<patch1 information>": "<patch1 file path>"
        },
         "drupal/libraries": {
            "<patch1 information>": "<patch1 file path>"
        }
     }
}

In the example above, we are patching to different modules, the Drupal core and libraries modules. Each separate module are defined separately on each line.

Then run:

composer install

After composer install, some time composer.lock file might not updated with latest patch information. We can run the command below:

composer update --lock

This will update your composer.lock file and include any newer updates made to composer.json file.

If there are multiple of patch files you want to include for the 'same' module, you group them together separated by a comma, like:

"extra": {
    "enable-patching": true,
    "patches": {
        "drupal/core": {
            "<patch1 information>": "<patch1 file path>",
            "<patch2 information>": "<patch2 file path>"
        }
     }
}

Applying a patch file stored locally

You can also download a patch to a directory and apply it locally, just create a directory name 'patches' just outside of your Drupal web root directory and download a patch file to the /patches directory. You can then add a relative path, like:

"extra": {
    "enable-patching": true,
    "patches":
{
    "drupal/module": {
        "example for any custom/contrib module": "patches/module-0000.patch"
    }
}

It could be a good idea if you created a patch and want to test it before submitting the patch to the module's issue queue.

That is all there is to it.

Here is a real working example of what a composer.json file would compromise of for adding patch file to the libraries module (the patch file was submitted at https://www.drupal.org/project/libraries/issues/3119010):

{
    "name": "drupal/recommended-project",
    "description": "Project template for Drupal 8 projects with a relocated document root",
    "type": "project",
    "license": "GPL-2.0-or-later",
    "homepage": "https://www.drupal.org/project/drupal",
    "support": {
        "docs": "https://www.drupal.org/docs/user_guide/en/index.html",
        "chat": "https://www.drupal.org/node/314178"
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        }
    ],
    "require": {
        "cweagans/composer-patches": "^1.6",
        "drupal/libraries": "3.x-dev",
    },
    "conflict": {
        "drupal/drupal": "*"
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "config": {
        "sort-packages": true
    },
   "extra": {
        "enable-patching": true,
        "patches": {
            "drupal/libraries": {
                "3119010: Drupal 9 Deprecated Code Report": "https://www.drupal.org/files/issues/2020-05-26/3119010-14_0.patch"
        }
    },
        "drupal-scaffold": {
            "locations": {
                "web-root": "web/"
            },
            "file-mapping": {
                "[web-root]/sites/development.services.yml": false
            }
        },
        "installer-types": ["npm-asset", "bower-asset"],
        "installer-paths": {
            "web/core": ["type:drupal-core"],
            "web/libraries/slick": ["npm-asset/slick-carousel"],
            "web/libraries/spectrum": ["custom-asset/spectrum"],
            "web/libraries/{$name}": [
              "type:drupal-library",
              "type:npm-asset",
              "type:bower-asset"
            ],
            "web/modules/contrib/{$name}": ["type:drupal-module"],
            "web/profiles/contrib/{$name}": ["type:drupal-profile"],
            "web/themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/Commands/contrib/{$name}": ["type:drupal-drush"],
            "web/modules/custom/{$name}": ["type:drupal-custom-module"],
            "web/themes/custom/{$name}": ["type:drupal-custom-theme"]
        },
        "drupal-core-project-message": {
            "include-keys": [
                "homepage",
                "support"
            ],
            "post-create-project-cmd-message": [
                "<bg=blue;fg=white>                                                         </>",
                "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",
                "<bg=blue;fg=white>  from the drupal/recommended-project template!          </>",
                "<bg=blue;fg=white>                                                         </>",
                "",
                "<bg=yellow;fg=black>Next steps</>:",
                "  * Install the site: https://www.drupal.org/docs/8/install",
                "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",
                "  * Get support: https://www.drupal.org/support",
                "  * Get involved with the Drupal community:",
                "      https://www.drupal.org/getting-involved",
                "  * Remove the plugin that prints this message:",
                "      composer remove drupal/core-project-message"
            ]
        }
    }
}

Another ways

You can revert a patch with:

$ git apply -R <patch>

You can generate a patch either by one of the following:

This will generate a patch from a diff

$ git diff --patch > 0001-some-modifications.patch

If you want to generate a patch for just the HEAD commit:

$ git show --patch HEAD^ > 0001-some-modifications.patch

You can generate a patch for the previous 3 commits from HEAD:

$ git show --patch HEAD~3 > 0001-some-modifications.patch

You can apply the patch by:

$ git apply -- 0001-some-modifications.patch

You can revert a patch with:

$ git apply -R <patch>

When you generate a patch it is just a diff with metadata; files, line numbers adds/removes; something along the following:

commit 9dad147cbf16befecdef2e812c1249499bdef5ac
Author: My Name <[email protected]>
Date:   Mon Dec 21 20:46:01 2015 +0000

Example commit message.

diff --git a/src/example.md b/src/example.md
new file mode 100644
index 0000000..ab73512
--- /dev/null
+++ b/src/example.md
@@ -0,0 +1,3 @@
+# Example document
+
+ Hello World

So when you use git apply you're essentially applying the edits as per to the tree.

When you then run git apply -R git will simply do the opposite to the patch.

Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

Master list (in progress) of how to get parts of fields for use in Twig templates. I’m always having to look these up, so I thought I’d hash them out and write them down.

Litespeed Cache là plugin WordPress dùng để kết hợp với Web Server LiteSpeed nhằm tăng tốc website WordPress của bạn gấp nhiều lần

In this article, we are going to see how some tools & libraries will make people's lives easier during the development & code review process.

In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git

Trước khi tìm hiểu xem PHP Code Sniffer là gì thì các bạn cần phải nắm được coding convention là gì đã.