How to apply patches in Drupal 8 with Composer

How to apply patches in Drupal 8 with Composer

Posted on Sun, 04/12/2020 - 21:06 by yuseferi

Sometimes you need to apply the patch in Drupal 8. as I wrote here after Drupal 8.8, Drupal is more composer friendly CMSF, So you can easily apply custom patches without worry about how to keep the patches available in Drupal core updates.

 

How to apply a custom patch with composer?

first, you need to install cweagans/composer-patches which is a package to apply patches by composer. you can install it with 

composer req cweagans/composer-patches

 

Then, find the patch file or URL on the Drupal.org website.

 

then edit your composer.json file, in the extra section if you don't have patch section add with patch element; if not just, add a record to patch section.

 

 

        "patches": {
            "drupal/YOUR_CUSTOM_MODULE_OR_CORE": {
                "YOUR_CUSTOM_NAME": "URL_OF_PATCH_FILE"     
            }
        }

 

for an example I apply custom patch from Consumer module

 

 

        "patches": {
            "drupal/consumer": {
                "Consumper update ": "https://www.drupal.org/files/issues/2019-02-09/3031068--entity-definition-after-owner-trait--11.patch"
            }
        }

 

then run `composer update --lock` to update the composer.lock file with the latest changes on the  composer.json file. Finally, run composer install on the command line to the custom patch will be download and apply automatically.

 

 

leave your questions in the command,