WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Creating a patch to a Drupal contrib module

Submitted by barnettech on Wed, 05/29/2024 - 16:52
To apply a patch from an existing merge request on Drupal's GitLab repository to your project while waiting for it to be merged, you can follow these steps:

1. Download the Patch
From the merge request page (https://git.drupalcode.org/project/samlauth/-/merge_requests/18/diffs), you can usually find an option to download the diff or patch directly. If you're viewing the merge request:

Click on the "Changes" tab.
Right-click the "Download" button at the top right of the diff display and select "Save Link As..." to download the patch file.
2. Save the Patch Locally
Save the patch file in a directory within your project. A common practice is to store them in a directory named patches at the root of your project:

plaintext
Copy code
patches/fix-authmap-delete-link-samlauth.patch
3. Modify Your composer.json
You will need to tell Composer how to apply the patch. If you haven't already, you need to require the cweagans/composer-patches plugin which is used to apply patches. Here’s how you do that:

bash
Copy code
composer require cweagans/composer-patches
Then, modify your composer.json to include the patch in the extra section:

json
Copy code
"extra": {
    "patches": {
        "drupal/samlauth": {
            "Description of the patch": "patches/fix-authmap-delete-link-samlauth.patch"
        }
    }
}
Here’s a breakdown of the composer.json modifications:

drupal/samlauth: This is the package name.
Description of the patch: This can be anything descriptive.
patches/fix-authmap-delete-link-samlauth.patch: The relative path from the root of your project to the patch file.
4. Apply the Patch
Once you've saved your composer.json, run:

bash
Copy code
composer install
Composer will attempt to apply the patches specified. If successful, you’ll see output indicating that the patch has been applied.