How to Create Drupal Patches — a way to give back to the community.

Written by
Published
Updated
Typical Read
3 minutes

Up your Drupal game by contributing back to core & modules using Drupal patches. Found a bug, wanna integrate a new feature or maybe you need something specific modified just for your site. In this article, learn how to create patches for Drupal.

Go from Drupal novice to Drupal god by contributing back to the open-source community with patches. Drupal patches are a key component to how the Drupal ecosystem continuously grows to become a better, more modern CMS.

What is a a Drupal patch file? A patch file is basically a text file with the extension .patch. It includes formatted code in a particular language extracted using git diff. Drupal patch files allow code changes to core & modules such as fixing bugs, improving performance issues or adding new features.

Guidelines for Creating Drupal Patches

  1. Keep things organized
    To help reviewers understand the scope of changes, separate each change type into its own patch. For example, bug fixes, performance enhancements, code style fixes, and whitespace fixes all should be in different patches. Each separate change type (and patch) should be associated with a different issue in the queue on Drupal.org.
  2. Pay attention to line endings & directory separators
    Note for Windows users: Use Unix line endings (LF) and directory separators (/). Many text editors can convert line endings, or you can pipe diff output through dos2unix.

Creating a Patch for a Drupal Module

Step 1: Clone the Drupal Module

The first step is cloning the module. If you don’t already have Git installed, you’re going to need to install it. Once you have, head over to the module’s project page (ex. https://drupal.org/project/seckit).

Security Kit
https://drupal.org/project/seckit

Click “Version Control” under the title (ex. https://drupal.org/project/seckit/git-instructions). Where it says, “Version to work from”, select the version of the module you’re wanting to patch. In this case, I want to patch 7.x-1.8, so selected “7.x-1.x” and then clicked “Show”.

Drupal Module Version

Now open up a terminal window with Git installed and clone the project’s repo. In this example:

This will copy the project’s files onto your system so you can make the updates needed.

Step 2: Update the Module

Since I needed to increase the maxlength limit for the allowed source fields, I opened: sites/all/modules/contrib/seckit/includes/seckit.form.inc which includes the admin form settings for that module’s configuration page. There I found that they weren’t defining the maxlength for the fields, so defaulted to the 128 character limit.

This is easily fixed by adding the #maxlength option to the fields array. See the example below:

For more information on how Drupal handles form fields, see their Form API Reference docs.

Step 3: Create the Patch

Now that you’ve made the changes to the module, you’ll need to create the patch for it. This is easily accomplished with Git. Open terminal and go into the module’s directory:

With this being a simple update, the following command will work for most improvements:

In my case, there hadn’t been an issue created for my problem yet, so I did:

For more complex improvements that require adding/removing files, work over the course of multiple days including Git commits, or collaboration with others, see the Advanced patch workflow.

This will create the patch file you need in the module’s directory. Pretty simple, huh? So, what about applying a patch?

Appyling a Patch to a Drupal Module

Applying a patch is as simple as adding the patch file to your module’s directory, then running the following command:

In my case, I did:

Once the patch has been applied, be sure to remove it so it doesn’t accidentally get included in future commits:

Pretty simple stuff!


I’d love to hear any feedback, suggestions or questions. Keep in mind, I’m fairly new to Drupal so would love to hear what the veterans think.

Join the conversation.

Your email address will not be published.

All comments posted on 'How to Create Drupal Patches — a way to give back to the community.' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.