How to apply a Drupal / contrib patch with the cweagans/composer-patches package

Jul
2020
02

Applying patches in drupal 8 is different compared to drupal 7.

Composer does a decent job alleviating the headaches of managing several sites with different dependencies. However, out of the box Composer will revert patched core files and contrib modules and it is for that reason composer-patches project was created. In this blog post, we are going to review how to set up composer-patches for a composer managed project and how to specify local or remote hosted patches. [from https://www.mediacurrent.com/blog/composer-patches-dependency-your-proje...

If you have not yet installed then you should first install the cweagans/composer-patches package (https://github.com/cweagans/composer-patches) via composer

composer require cweagans/composer-patches:~1.0 --update-with-dependencies

Now after this you can open your /root/composer.json file and add this section within the extra section like this

   ...
   "extra": {
        "drupal-scaffold": {
            "locations": {
                "web-root": "web/"
            }
        },//This is where you apply the patches
        "enable-patching": true,
        "patches": {
            "drupal/username_policy": {
                "username policy default value issue": "https://www.drupal.org/files/issues/2019-12-20/username_policy_default_username_patch_0.patch"
            },
            "drupal/core": {
                "Fix Admin Toolbar Language Issue": "https://www.drupal.org/files/issues/2018-04-12/2313309-44.patch",
                "FIx Permissions Translate Content Can Edit Issue": "https://www.drupal.org/files/issues/2019-12-03/allow-users-to-translate-editable-content-2972308-41.patch"
            }
         },

The format for each patch is : “Patch description”: “path to patch file”

Run composer validate in order to make sure your json format is valid, and then run composer update

If everything is ok you should see the applying patches for drupal/core message with success

Good Luck!
this is an arrow pointing back to the top of the page