
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 CautionBefore 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 ErrorImaginee 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: Research the errorI 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 solutionIf 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 patchedAfter 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.
![]() Download the file to be patchedOpen the modules folder, then the user folder and locate user.module. ![]() Open the file and find the code to patchOpen 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 ![]() Patch the codeApply 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 testSave 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. SummaryThe 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. |

With Admincredible you can update and manage all your Joomla sites. If you have 5 or 500 sites, Admincredible will make your life easier! Visit Admincredible.com.
All of our tutorials are published under the Creative Commons Attribution-NonCommercial license. This means:
Open Source Training is not affiliated with or endorsed by the Joomla, WordPress or Drupal projects.
All product names and trademarks are the property of their respective owners.
Copyright 2013 Open Source Training, LLC. All rights reserved.
Comments
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!
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.
Link to that module: drupal.org/.../patch_manager
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).
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! :)
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.
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.