Thumbnail

Patching a Composer project

Thumbnail

Si Hobbs

|

Composer Patches makes it a breeze to patch your Drupal project on the fly.

Most Drupal 8 projects do not keep Drupal contributed modules in the repository, they are downloaded during the build in composer install. So how do you manage patches?

Prior to composer builds, it was common to hack a module, and then store a patch someone, perhaps a patches directory. Admittedly drush make could do something similar, but it wasn't commonly used.

Drupal 8, we need to apply the patch on the fly, and cweagans/composer-patches is our friend. This isn't new, but there are some tips to make things easier if you're just getting started.

The basics

Composer-patches will be one of the requirements in your composer.json, and you can learn more about using and configuring it on the project's Github page.

The same below should make intuitive sense.

{
  "require": {
    "cweagans/composer-patches": "~1.0",
    "drupal/some_module": "~1.0"
    ...
  },
  ...
  "extra": {
    "patches": {
      "drupal/some_module": {
        "Fix the WSOD on the /admin page": "https://www.drupal.org/files/issues/some-thing-1543858-30.patch"
      }
    }
  }
}

Use existing patches

The ideal situation is finding an existing patch in a public issue queue on places like drupal.org or Github. Conveniently, Drupal.org patches has the node ID and comment ID in the patch name, so you can easily trace a patch back to its source.

In some cases there might not be a convenient patch URL. You can store the patch in your repository (eg. a ./patches directory), and apply it locally.

    "extra": {
        "patches": {
            "drupal/block_class": {
                "Fix the WSOD on the /admin page": "local-patch-directory/ignore-that-setting.patch"
            }
        }
    }

Quick patch generation

Patches are often the by-product of bug fixes and improvements that haven't been released yet. However sometimes you need a patch which is either speculative or temporary. You might want to disable a broken feature until you have more time to work on it. Or you are fixing a bug, but you're not sure if your approach is correct.

Existing Github commits

You can create a patch from Github by adding ".patch" to the end of a commit or a pull request. Say you've added a stable version of pendashteh/taskrunner, but there is recent a commit you want from the master branch. Just add .patch to the commit URL. This URL is perfectly usable by composer-patches.

Fork and commit

You only need a commit in a public Github repository to create a patch, then you can achieve this by forking a project to your own Github account. You may not need to clone the project locally, Github's in-browser editing tools may be enough. 

Mirroring Drupal.org projects

For Drupal.org projects there is a service called Hubdrop that can mirror Drupal.org modules and themes to Github. If a module is not already mirrored, Hubdrop will do it for you in a few minutes. Once the project is mirrored, you can fork it and create a patch. 

To see an example of this process end-to-end, check out my video below.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Comments

  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <ul type> <ol start type> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
  • Use [gist:#####] where ##### is your gist number to embed the gist
    You may also include a specific file within a multi-file gist with [gist:####:my_file].

Spread the word