How to apply a Drupal / contrib patch with the cweagans/composer-patches package
cweagans/composer-patches is simple patches plugin for Composer. Applies a patch from a local or remote file to any package required with composer.
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
Now after this you can open your /root/composer.json file and add this section within the extra section like this.
Example How to use this package with drupal project:
...
"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.
Others examples:
Example composer.json:
{
"require": {
"cweagans/composer-patches": "~1.0",
"drupal/core-recommended": "^8.8",
},
"config": {
"preferred-install": "source"
},
"extra": {
"patches": {
"drupal/core": {
"Add startup configuration for PHP server": "https://www.drupal.org/files/issues/add_a_startup-1543858-30.patch"
}
}
}
}
Using an external patch file
Instead of a patches key in your root composer.json, use a patches-file key.
{
"require": {
"cweagans/composer-patches": "~1.0",
"drupal/core-recommended": "^8.8",
},
"config": {
"preferred-install": "source"
},
"extra": {
"patches-file": "local/path/to/your/composer.patches.json"
}
}
Then your composer.patches.json
should look like this:
{
"patches": {
"vendor/project": {
"Patch title": "http://example.com/url/to/patch.patch"
}
}
}