How to apply Drupal patches with Composer

When working with Drupal you will, sooner rather than later, have to apply a patch on some project.

To make this work, you first need to require the cweagans/composer-patches package:

composer require cweagans/composer-patches

After this package is installed, add the following lines to the composer.json file:

"extra": {
    "composer-exit-on-patch-failure": true,
    "enable-patching": true,
    "patchLevel": {
        "drupal/core": "-p2"
    "patches": {
        "drupal/core": {
            "patch information": "patch url or path"

And run:

composer install

Here's an example of my composer.json file for a project I am working on: I usually add the issue name and link to the comment as "patch information" for easier reference.