Colorfield logo


Published on

Patch a Drupal project

Photo by Murat Onder on Unsplash

This assumes a Composer based setup.

1/ Require Composer patches

composer require cweagans/composer-patches

2/ On the extra section from composer.json, add

"patchLevel": {
  "drupal/core": "-p2"
"enable-patching": true,
"composer-exit-on-patch-failure": true,
"patches": {},

Here is an article about patchLevel.

3/ Add then a patch for a Drupal project (core or contributed) in the patches sections, where we give the project reference: drupal/core or drupal/my_module. Each project can contain a list of key values with

  • a label, e.g. prefixed with the issue number
  • where to find the patch: an absolute url or a local patch in e.g. a patches directory
"patches" : {
  "drupal/core": {
    "[#2982052] Allow an install hook in profiles installing from configuration": ""

4/ The patch will apply with composer install, a preferred method is to use

composer update --lock

that will also update the lock file.

What about libraries?

Given that there is a type package of type "drupal-library" in the "repositories" section.


  "type": "package",
  "package": {
    "name": "harvesthq/chosen",
    "version": "1.8.2",
    "type": "drupal-library",
    "dist": {
      "url": "",
      "type": "zip"
    "require": {
      "composer/installers": "^1.2.0"

the patches section will then contain the package name ("harvesthq/chosen" in this case).

... and about Node packages?

The same principle can be applied in package.json with Patch Package.

npm i patch-package

then, in the postinstall script, add the directory that contains the patches (a different directory than Drupal sounds like a good idea).

"postinstall": "patch-package --patch-dir=patches"

Continue reading