How to apply patches in Drupal 8 with Composer

Wed, 08/10/2016 - 06:57 | 12721 Views

How I mentioned in my previous post How to add ReactJS in Drupal 8 with Composer, Composer is a fundamental part of Drupal 8 development workflow.

Like any open source projects, contributions are the core of Drupal, and those contributions are included in Drupal modules and themes and event Drupal core via Patches.

1. Drupal Installation.

In this example I will assume that did you install Drupal using *Drupal Composer* project, using an instruction similar to the following:

$ composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interact

2. Installing module to patch.

I will use the module Address to demonstrate how to patch a module.

Using the following module we could install the Address module in our Drupal 8 project.

$ composer require "drupal/address ~8.1"

3. Patching the module.


The Address module works pretty well, but there is an issue related to full configuration import in a site, this issue was reported and fixed in https://www.drupal.org/node/2663412. But at the moment of this article that solution wasn't included in the stable release of Address module.

Including composer lingua, we could say module and themes are packages and their difference remains in package type. To patch a package is need to edit put composer.json file to provide the patch instructions as you can see in the following snippet of code.

"extra": {
        "installer-paths": {
            "web/core": ["type:drupal-core"],
            "web/libraries/{$name}": ["type:drupal-library"],
            "web/modules/contrib/{$name}": ["type:drupal-module"],
            "web/profiles/contrib/{$name}": ["type:drupal-profile"],
            "web/themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/contrib/{$name}": ["type:drupal-drush"]
        },
        "patches": {
            "drupal/address": {
                "Drupal Addess fix default syncing": "https://www.drupal.org/files/issues/address_syncing.patch"     
            }
        }
    }

As you could see, the format is a straightforward entry in patches group, providing the package to patch and the URL of the patch to be download with a human comment.

4. Applying your patches.

The next time do you run *composer install* or *composer update*; your patches will be applied, and you will get an output similar to the next image.

Applying patches

I recommend checking the slides Improving your Drupal 8 development workflow http://weknowinc.com/talks/2016/drupalgov-workflow for more references about how to use Composer in Drupal 8 projects