Subscribe To Our NewsLetter
Share This Post:
Have you ever faced any problems on your Drupal websites coming from Drupal core, its contributed modules and themes or do you want to enhance their functionality as per your requirement which can't be possible through your custom modules? As you know we can't apply code directly there, then what could be the solution for this? Well, patching those codes might just be the solution you're looking for.
In this blog, we'll talk about the process of creating and applying patches using git diff and git apply commands and we will also apply patches through composer install command. By applying patches you can achieve all your requirements before this you must have good coding skills to understand Drupal code.
Understanding Git Patches
Let's start to understand the patches first. A patch is like a set of instructions that tells your website how to change its code. It's needed when you want to fix any bug or add a new feature without changing the original code directly which we can't do in core, contributed modules and theme. Instead, you make your changes, create a patch file, and then apply that to your website code.
How To Make Changes To the File
The first thing you have to do is verify what you require to change on your Drupal website, whether it could be fixing a bug or adding a new feature. Once you have known about this then open that file or directory as per your requirement in a separate IDE (VS Code Sublime) and initialize git there through the command:
git init
Now add and commit those files/directories through the command
git add .
git commit -m "first commit"
How To Create A Patch
Now it's time to make your changes as per your requirement, When you are satisfied with your changes, now you are ready to create a patch file. Hit the following command.
git diff > functionality_fixes.patch
git diff command compares the changes you have made to the original files and saves those differences to a file called functionality_fixes.patch. You can any name to your patch file it can be anything that reflects its working for better understanding, just make sure it ends with .patch extension. Now create a directory named Patches on the root directory of your project and place your patch file in this.
Let's review a patch file
Please note that the path in the patch file must match the absolute path where you want to apply that patch. For example, if you want to apply the patch on AdminToolbarSettingsForm.php of the Admin Toolbar module the path in the patch file will be like
/modules/contrib/admin_toolbar/src/Form/AdminToolbarSettingsForm.php
you do not mention the right path composer will fail to recognise file and the patch will fail.
How To Apply Patch through Composer
The preferable way to apply a patch on Drupal projects is through Composer for this you first need to install cweagans/composer-patches through the command
composer require cweagans/composer-patches
Now open the composer.json file and write about Patch details there so that your patch can be installed whenever the composer install command gets hit. Below is an example of syntax how we can write those details
"extra": {
"patches":{
"drupal/admin_toolbar": {
"Functionality fixed in admin toolbar setting form": "patch/functionality_fixes.patch"
}
}
}
In the above example, I have applied a patch on the contributed admin toolbar, similarly, you can do this for any module, core, and theme. After saving those details please hit the following command.
composer install
If the patch is applied successfully, you will see the message of confirmation during installation.
Let’s Wrap It Up!
Mastering the art of patching in Drupal can significantly enhance your ability to customize and extend the functionality of your websites. Whether you encounter issues stemming from Drupal core, contributed modules, or themes, or if you aspire to implement bespoke features beyond the capabilities of custom modules, patching provides a viable solution. Through the diligent use of commands like git diff, git apply, and incorporating patches via composer install, you can tailor your Drupal environment to meet your precise specifications.
At LN Webworks, our certified Drupal experts are always ready to help you. Get in touch with us right now to schedule a free consultation.
Share This Post:
Author Information
LN Webworks
Your Drupal Solution PartnerLN Webworks have championed open-source technologies for nearly a decade, bringing advanced engineering capabilities and agile practices to some of the biggest names across media, entertainment, education, travel, hospitality, telecommunications and other industries.