Áp dụng Drupal Patches mà không Coders

Áp dụng Drupal Patches mà không 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:

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>

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

media_1359991432096.png

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.

media_1359991480683.png

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.

media_1359991681534.png

Download the file to be patched

Open the modules folder, then the user folder and locate user.module.

media_1359991844952.png

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

media_1359992120726.png

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.

media_1359992425316.png

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.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Drupal 8 Configuration Workflows sử dụng Git

Drupal 8 Configuration Workflows sử dụng Git

This blog post is a textual representation of the video shared yesterday. If you are visual learner, watch it. If you are in a hurry, read this blog :). Peter's video also shows how configuration_log module can be used to materialize all config changes in Prod so they may be easily integrated back into the codebase.

iPhone 4 phát lửa ngay sát mặt người

iPhone 4 phát lửa ngay sát mặt người

Điện thoại của Apple bất ngờ bốc khói khi sạc qua đêm trong khi chủ nhân đang nằm ngủ cách đó chỉ khoảng 15 cm.

SEO

Chợ Điện Tử và thủ thuật SEO mũ đen

Chuyện gì xảy ra khi Chợ Điện Tử, một ông trùm thương mại điện tử, dùng tiền để nhanh chóng khẳng định sức mạnh của mình trên các máy tìm kiếm với các thủ thuật seo mũ đen. Liệu họ có thành công với phương pháp của mình?

Công ty diệt chuột T&C

 

Diet con trung