Applying Drupal Patches (for Non-Coders)
Have you ever updated your Drupal site only to suddenly have errors?
If you use Drupal regularly, this will happen to you at some point. However, one of the good things about using Drupal is there are so many other users that someone else may well have found and solved the error.
One common way to solve an error is with a patch. A patch changes the code on your site, but only by editing a file rather than providing a complete update.
Many of the available instructions for applying patches ask you to use an application called Git and to use command line instructions. These instructions can be intimidating, so we’re going to show you how non-coders can safely and effectively apply patches.
A Note of Caution
Before we start, it’s important to note that this is not the ideal solution for applying patches. Drupal has instructions for more reliable ways to apply patches: http://drupal.org/patch/apply.
Also, please make sure that before you do this that you have a backup of your site and that you test the patch on a backup installation of your site.
The technique in this blog is strictly for non-coder who are stuck in charge of a Drupal site that they need to fix.
The Error
Imaginee you have just installed Drupal and you have created your first user account but something goes wrong. You get an error that looks like this:
Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 10 in user_validate_name() (line 637 of /home/user/mysitename.com/html/modules/user/user.module).
Research the error
I went to Drupal and searched for the error message. I seearched for this string: ‘Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 10’
This search produced several results.
One search result was http://drupal.org/node/820366 and although the original post references Drupal 6, you can see that the issue was switched to Drupal 7. This switch sometimes happens when the same issue is present in two versions of Drupal.
The good news is that someone has posted a patch for Drupal 7: http://drupal.org/node/820366#comment-3082772
Assessing the solution
If you click on the link to the patch, you’ll see code as in the screenshot below.
If this is the first time you have looked at code, let alone a patch, you might feel like you are in over your head but trust me, it isn’t that bad.
The first thing you want to note is the file that needs to be changed. In this instance, we need to edit a module in Drupal’s core. Please note that we are not hacking core – we are patching it. The difference is that a hack is designed to permanent whereas with a patch, we hope that a future version of Drupal will fix this bug.
I recommend keeping track of all the patches you apply to your site code. You could do something as simple as keeping a text file with a list of all the patches you have applied and their source.
Locate the code that needs to be patched
After you have found the patch, now you need to find the code that needs to be patched. Let’s assume you do not have a copy of your site code on your local computer. This means you need to find it on your server.
The screen shot below is from the FileZilla SFTP tool. The arrows indicate the folder where you will find Drupal’s core modules.
How do you know to look here? The first line in the patch told you — modules/user/user.module.
Below are tips to help you find the code you seek for other patches.
- If you are patching code for a contributed module that you added to your site, start by looking in the sites > all > modules folder.
- If you are patching code for a core module, look in the first modules folder you see. It will be at the same level as the sites folder.
- If you are patching code for a contributed module that came with an alternative distribution of Drupal, try looking in profiles folder.
Download the file to be patched
Open the modules folder, then the user folder and locate user.module.
Open the file and find the code to patch
Open the file in a simple text editor. In the screen shot below, the file was opened in WordPad. Observe the highlighted line of code. This line matches the line of code with a minus in front of it (see bold text in patch below)
diff –git modules/user/user.module modules/user/user.module
index bebfa88..ea3af50 100644
— modules/user/user.module
+++ modules/user/user.module
@@ -586,7 +586,7 @@ function user_validate_name($name) {
if (strpos($name, ‘ ‘) !== FALSE) {
return t(‘The username cannot contain multiple spaces in a row.’);
}
– if (preg_match(‘/[^\x{80}-\x{F7} a-z0-9@_.\’-]/i’, $name)) {
+ if (preg_match(‘/[^\x{80}-\x{F7} a-z0-9@_.\’-]/ui’, $name)) {
return t(‘The username contains an illegal character.’);
}
if (preg_match(‘/[\x{80}-\x{A0}’ . // Non-printable ISO-8859-1 + NBSP
Patch the code
Apply the new code.
The minus/plus symbols indicate the line of code to remove and the line of code to put in its place. Notice in the screen shot below that the highlighted line of code is now different and matches the code next to the plus in the patch.
Save, upload and test
Save the file and upload it to the server, replacing the original file. Return to your site and try creating another user account and see if the error goes away. If it does, consider posting your results on the issue so that the person who created the patch will know it worked.
Summary
The example we just applied was fairly simple. We replaced one line of code for another. Sometimes patches will remove multiple lines of code and replace them with one and visa versa. The trick is to pay attention to the -/+. Also, some patches reference multiple files so please read the patch carefully.
No offence and I’m sure there’s some who will appreciate this but this is just terrible practice.
Manually updating code like that is error prone and guaranteed to break things eventually. If the terminal intimidates you, It is very simple to apply patches using either a version control IDE or by copy & pasting a command in the terminal.
It’s SO simple, really. I’ve read instructions like these in the PhpBB forums but not Drupal, please!
Agree. While I did use this method a couple of years ago on a simple patch when I was starting (which am grateful as it removed the “mystery” of what a patch was).
Now I just use an even simpler or more straight forward set of commands like this on my linux server or vps:
1- CD into the specific module directory
2- get the patch file and apply it, i.e: “wget file.patch && patch -p1 < file.patch" quick instructions here [url=http://stackoverflow.com/questions/5577944/appliying-patch-in-drupal-modules]http://stackoverflow.com/qu...[/url]
Hi Alex,
Yes, it’s not ideal, but I do think it’s worth publishing this.
On the one hand it’s not ideal and thank you for pointing out the better options.
However, it’s worth realizing that there’s an entire ecosystem of non-coders who have been left in charge of Drupal sites. They don’t know how to use an IDE or the command line and might potentially do more damage to their sites that way.
This tutorial was written for one of our students who did indeed recoil after looking at the IDE and command line tutorials they’d found.
For non coders, use the patch manager module
Thanks Nigel,
Link to that module: [url=http://drupal.org/project/patch_manager]http://drupal.org/project/p…[/url]
I’m guessing I should update the blog to make it clearer that our recommendation above is for a certain group of user (non-coders) stuck in a certain situation (their site is throwing errors).
Hi Steve,
Yeah I realise that there’s a huge audience of non-technical folk that work with Drupal on a daily basis and I’m sure this article will prove helpful to a lot.
I might not have expressed myself very well but my point was just that in my opinion it’s better to learn how to do something the “proper” way than to disseminate and perpetuate hackish workarounds.
Either way, I apologise if I came off rudely and if I discouraged your student!
All of us are always learning and it takes a lot of courage and show initiative to write a post like this and share info with the world! 🙂
No worries at all, we really appreciate the feedback Alex.
We’ve modified the blog post as a result. We added a disclaimer at the top reflecting exactly what we’ve discussed in the comments.
I found applying patches using NetBeans very easy and useful: [url=http://drupal.org/node/60179]http://drupal.org/node/60179[/url]
The real value of this tutorial is that it opens up the patch code and shows it’s simple inner workings: just adding and subtracting lines from a file. There are better ways to fix a broken arm than with sticks and duct tape. But if you fix the first one, you are ready to pick up better tools for the second one.
I realized that this practice is far from ideal, BUT, there are very few resources out there from the Drupal newbie.
And I mean the absolutely newbie.
And usually the people of good will that reply is far too technical sometimes. Or they just assume that the person that asks knows a lot of stuff that they just don´t.
I still remember how terrified I was the first time that I found myself in need of applying a patch to a module.
I would like a second part of this tutorial telling people how to apply a patch with tortoise, in windows.
Leave the man be, thanks to him my site that was down due to a bad bug in ctools that conflicts with quicktabs is gone, i tried everything from cygwin tortoiesvn till this, not even drupals documental on a manual patch is this detailed, you sir have my gratitude
I fixed the patch in a few minutes,
Thank you very much!!!!!!!!!!!!!!!
.. I do not need Git oder similar program to use ! 😉
I agree with Lauren, and I think this is excellent, it gives me a better understanding of what the code means, it might not be the “right” way to do it, but it does help a lot…
I think this site is awesome! Thanks guys!!
Thanks Mike. Glad it was helpful.
I may have failed to comprehend what the over 30+ threads on “how to apply a patch in drupal” were telling me… but this one particular thread by OS Training got me successfully applying the patch quickly. Thanks guys for this!
i love this approach to do the patch!