Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
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.
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.
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:
1.<i>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).</i>
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
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.
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.
Open the modules folder, then the user folder and locate user.module.
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
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 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.
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.