How to Apply Patches in Drupal 8 & 9 with Composer

Apply patches in Drupal 8/9 with composer-patches plugin, then specifying URL of patch file (typically from drupal.org) in composer.json
  1. Install Composer https://getcomposer.org/download/
  2. Install Drupal and desired modules using Composer drupal.org guide composer create-project drupal/recommended-project
  3. Install relevant modules, for example composer require drupal/yearonly
  4. Install Cameron Eagans patches plugin using composer require cweagans/composer-patches
  5. Edit composer.json extra section and add in the the patches section immediately after "installer-paths" as below, using the URL of the patch file [from drupal.org]:
    "patches":
      {
        "drupal/yearonly": {
          "Token types missing name property": "https://www.drupal.org/files/issues/2019-05-02/token_missing-2951179-4.patch"
        },
      },
    "enable-patching": true,
  6. Run composer update to apply patches - Composer will delete the modules to be patched, then re-download them with patches applied.
  7. Add more patches to composer.json as required. You can remove these entries if the module is released with an official patch

 

There's a detailed guide on installing patches to Drupal 8 with Composer, but has a crucial missing step at https://www.anexusit.com/blog/how-to-apply-patches-drupal-8-composer