megclaypool of Rootid
3/13/2019 - 4:47 PM

How to apply patches in Drupal 8 with Composer

How to apply patches in Drupal 8 with Composer

  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. Make sure you have the cweagans/composer-patches composer plugin installed:
    composer require cweagans/composer-patches
  3. 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"
  4. 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. (Note: the installer paths are just there to show a sibling in the "extras" section, not as something you're supposed to add!)
"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.

  1. 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.