Patching Drupal Scaffold Files with composer-patches

Why not use post-drupal-scaffold-cmd?

It's possible to patch Drupal scaffold files by adding a post-drupal-scaffold-cmd section in composer.json. I did it this way for a long time.

But calling the shell patch command directly feels a bit clunky to me, and I'd rather group the patches in one section of composer.json.

Instead of doing it with post-drupal-scaffold-cmd, here's a method for applying patches to scaffold files using the cweagans/composer-patches package. Using the composer-patches method also allows a description for the patch that is displayed during composer install.

Note: This technique requires Drupal ^8.8.x, with this change.

How?

  1. Create the patch against the corresponding file in the core/assets/scaffold/files directory.
  2. Save the patch in your standard patches location.
  3. Add an entry under the drupal/core project in the patches section of composer.json.

Create the patch into your standard patches directory

Create a patches directory if you don't already have one. A standard location for this is in the root directory of your project.

This is the easiest method I've found for creating the patch:

  1. Clone the Drupal core repo to the branch / tag that you want.
  2. Edit the relevant files in core/assets/scaffold/files.
  3. From the root directory of this repo, run git diff > path/to/patches/dir/my-change.patch. Change the filename as needed.

Add a corresponding patch entry to composer.json

If you don't already have a patches section in composer.json, add one according to the composer-patches instructions.

Add a group under patches for drupal/core if necessary, and add an entry for your new patch. For example:

{
  ...
  "extra": {
    "patches": {
      "drupal/core": {
        "My amazing change": "patches/my-change.patch"
      }
    }
  }
}

Now, when you run composer install, the patch should be applied to the scaffold file before it is copied into place.

Tags

Add new comment