To how apply patch file for composer based Drupal 8/9

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"
            ]
        }
    }
}

 

Alain (not verified), 06 Jan 2022 - 1:57pm
Hello, thanks for the tutorial. I am building my drupal 9.2 website on Wamp 3.2 (Windows 10) with composer, drush and Git without issues. Then I required ceweagans/composer-patch, but patches cannot be installed, neither with full url or local reference. Errortells the' patch' command couldn't be found, so I added the location of the patch.exe in my user PATH env variable, but it doesnt work. I run out of ideas :D Many thanks
Mathieu (not verified), 01 May 2022 - 2:15pm
Very well explained
Annie Abraham (not verified), 23 Dec 2022 - 2:20pm
Thankyou., this helped me
The content of this field is kept private and will not be shown publicly.
Your email address will be kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.