Patch a Drupal project

Submitted by christophe on Mon, 05/07/2021 - 15:21
Drupal patch


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 Drupal.org 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": "https://www.drupal.org/files/issues/2021-06-11/2982052-68-Allow_install_hook_profiles_installing_from_configuration.patch"
  }
}

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.

Example:

{
  "type": "package",
  "package": {
    "name": "harvesthq/chosen",
    "version": "1.8.2",
    "type": "drupal-library",
    "dist": {
      "url": "https://github.com/harvesthq/chosen/releases/download/v1.8.2/chosen_v1.8.2.zip",
      "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


 

Photo by Murat Onder on Unsplash