Last updated February 14, 2015.

You can use a patch program to apply a patch, but you don't need to. Doing it manually is perfectly ok for smaller patches, and gives you a better idea of what's going on in your code.

As said in the main article, don't patch your live site directly! Instead, patch your test site, test it, then upload the changes.

Example

The given patch could begin with something like this,

diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc
index fb0664a..193c475 100644
--- a/modules/system/system.admin.inc
+++ b/modules/system/system.admin.inc

This tells you which file you need to edit: modules/system/system.admin.inc.

@@ -911,12 +911,6 @@
  * Array sorting callback; sorts modules or themes by their name.
  */
function system_sort_themes($a, $b) {
-  if ($a->is_default) {
-    return -1;
-  }
-  if ($b->is_default) {
-    return 1;
-  }
+ // added a comment
   return strcasecmp($a->info['name'], $b->info['name']);
}

Here is the first snippet that you need to change.
911,12 is the line number and number of lines of the snippet before the patch is applied.
911,6 is the line number and number of lines of the snippet after the patch is applied.
  • Lines beginning with "-" are deleted.
  • Lines beginning with "+" are added (without the "+", of course).
  • Lines beginning with nothing remain as they are. Their only purpose is to give you an idea where you are in the code.

Please note: If you want to be able to revert the patch with an automated tool at a later time, you should be careful that you don't insert additional linebreaks or comments.

On the other hand, you might want to mark the modifications with comments, to find them later on.

Finding modified code

If your code is checked out from git, you can use git to view the modifications.

Otherwise, you can download a clean copy of Drupal + contributed modules into a separate folder, and use a diff tool like WinMerge to compare at windows the clean download with your modified code. You can find same alternatives for WinMerge for linux in linuxalt and alternativeto, and for Mac here.

Comments

amysragbag’s picture

I am completely new to looking at code, so I'm still a little puzzled.

@@ -618,10 +618,6 @@ function _system_is_incompatible(&$incom

So this says to delete line 618 and replace it with something. What do you replace it with? What do the ,10 and ,6 mean?

What does it mean when the lines in the patch look like:
233c221

Where can I find more detailed instructions to read?

Thanks!

kidconcept’s picture

If you are new to looking at code, applying a patch manually is more work than you need to take on. I imagine you are likely to do more harm than good. There is an excellent beginners guide to patching written up here:

http://drupal.org/node/620014

eorr’s picture

These numbers are just pre-patch and post-patch line numbers. The minus and plus signs mean pre-patch and post-patch respectively. They don't have anything to do with deleting or adding lines, as they do outside of the @@ symbols.

The first number, -618,10, is the initial line number before the patch is applied. So the code referenced just after the 2nd @@ would start on line #618. 10 refers to the number of lines in the section prior to patching.

The 2nd number, +618,6 is the initial line number after the patch is applied. So in this case, function_system_is_incompatible would start on line 618 after the patch is applied. There would be only 6 lines of code in this section after patched as opposed to the original 10. As more functions are changed in a single patch, the gap between the pre-patch line # and the post-patch line # gets bigger.

donquixote’s picture

added an explanation in the article.

purplekitty’s picture

Thank you for these clear instructions with example! I successfully applied a patch manually for the first time!

Kebz’s picture

YAY!! My first time applying a patch... LOL

I've been a drupalgrammer for over a decade and I change/edit codes and themes quite frequently.... but I've never had to apply a patch.
I'm providing a "before & after" sample below to hopefully help others to understand it better. This worked for me and I now have a "green" status report... yay!!

This sample below is for the CURRENCY / TOKEN (in reference to this link > https://www.drupal.org/node/2234015) which I will post inside there too.

BEFORE: starting @ line 33, with 10 lines of code (re: -33,10)
starting @ line 33, remove the following code that equals to 10 lines total

33 'type' => 'text',
34 );
35 $tokens['currency']['sign'] = array(
36 'name' => t('Sign'),
37 'type' => 'text',
38 );
39 $tokens['currency']['title'] = array(
40 'name' => t('Name'),
41 'type' => 'text',
42 );

AFTER: starting @ line 33, with 12 lines of code (re: +33,12)
starting @ line 33, paste the following code that equals to 12 lines total

33 'type' => 'text',
34 );
35 $tokens['currency']['sign'] = array(
36 'description' => t('The sign of the currency.'),
37 'name' => t('Sign'),
38 'type' => 'text',
39 );
40 $tokens['currency']['title'] = array(
41 'description' => t('The name of the currency.'),
42 'name' => t('Name'),
43 'type' => 'text',
44 );

The actual patch file (https://www.drupal.org/files/issues/currency-missing-token-descriptions-...) reads as follows: (Be sure to remove the "+" sign before saving and uploading back up into the server.... and ALWAYS make a copy FIRST before making any changes to any files you tinker with. :) )

diff --git a/currency/currency.tokens.inc b/currency/currency.tokens.inc
index 4587a87..0ac2964 100644
--- a/currency/currency.tokens.inc
+++ b/currency/currency.tokens.inc
@@ -33,10 +33,12 @@ function currency_token_info() {
'type' => 'text',
);
$tokens['currency']['sign'] = array(
+ 'description' => t('The sign of the currency.'),
'name' => t('Sign'),
'type' => 'text',
);
$tokens['currency']['title'] = array(
+ 'description' => t('The name of the currency.'),
'name' => t('Name'),
'type' => 'text',
);

~Kebz

corry’s picture

thank you kebz.Worked perfectly

Kebz’s picture

You're welcome @Corry
sorry for the late response ... just now seeing it.

~Kebz

dfish17’s picture

Yikes, I just tried to apply a patch manually and am getting the following error. I tried to do a full restore of home directory and database and am still getting the error. I'm thinking I had an extra line break in there, but it's still giving me the error after restoring.

Parse error: syntax error, unexpected end of file in /home/karenl12/public_html/sites/all/modules/field_collection/field_collection.module on line 1510

Kebz’s picture

@Dfish17

Yikes indeed! Has your problem been resolved? If not, you may want to Google that error within the specific module you are having errors with.

Just an FYI, whenever I do a patch, I always save a copy of the original file so that I can revert back to it in case something goes wrong.
Were you able to save the original file?

~Kebz

n2itdesigns’s picture

This is my first time needing to add a patch.

Here is the patch I am trying to use for a popup modal form. The patch is suppose to allow the confirmation message to appear after the form is submitted.

diff --git a/modal_forms.pages.inc b/modal_forms.pages.inc
index b9b3817..a19101d 100644
--- a/modal_forms.pages.inc
+++ b/modal_forms.pages.inc
@@ -295,7 +295,7 @@ function modal_forms_view_webform($node, $js = NULL) {
   $output = ctools_modal_form_wrapper('webform_client_form_' . $node->nid, $form_state);

   if (!empty($form_state['executed'])) {
-    if (!isset($form_state['storage'])) {
+    if ($form_state['webform_completed']) {
       ctools_add_js('ajax-responder');
       // Handle confirmation message or redirect.
       if ('<confirmation>' == $node->webform['redirect_url']) {

The original code ended on line 278 so I tried added linebreaks until I reached line 295 and add the code in the but it came up with an error on line 301.

Then I looked into the code more and found this code on line 241 that corresponds with the code in the patch.

$output = ctools_modal_form_wrapper('webform_client_form_' . $node->nid, $form_state);

  if (!empty($form_state['executed'])) {
    if (!isset($form_state['storage'])) {
      ctools_add_js('ajax-responder');

From my understanding it should be 7 lines but this is only 5, however the 2 lines after this are:

      $output[] = ctools_modal_command_display($title, $text . ctools_ajax_text_button(t('Close'), 'modal_forms/nojs/dismiss', t('Close')));
      // @todo Add support for redirect. Require some magic.

If I'm not mistaken I need to remove if (!isset($form_state['storage'])) { from the code. When I do this though and try to open the popup form it brings up this error.

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /modal_forms/ajax/webform/6
StatusText: OK
ResponseText:
Parse error: syntax error, unexpected end of file in /home/ab4093/public_html/sites/all/modules/modal_forms/modal_forms.pages.inc on line 281

The only piece of code on line 281 is }

Any help would be greatly appreciated.

Kebz’s picture

When doing a patch manually, be sure to pay attention to the "-" and "+" signs

In your case ...
.... this is the line that you have to remove (... the minus(-) sign indicates to subtract)

- if (!isset($form_state['storage'])) {

... and this is the line that you will replace it with (... the plus (+) sign indicates to add)

+ if ($form_state['webform_completed']) {

Also, you should verify dates and versions of the file you have versus the patch file that it is trying to patch up ...(makes sense?)

What is the module and patch file? Do you have links?

Hope I was able to help.

Regards,
Kebz =)

~Kebz

n2itdesigns’s picture

It is for modal forms. Here is the link to the patch (https://www.drupal.org/node/2598962).

I've mentioned the issue on there too. I was using the wrong version for the patch initially, but after changing to the correct version and adding the patch a different issue came up. The first issue was that the confirmation message wouldn't show up on the form submission, and after the patch the form won't show up at all.

Vako’s picture

Drupal should have a patch update module built in to the core.

Kebz’s picture

@n2itdesigns
Sorry it's been a while since I posted.

Just checking to see if you were able to resolve your problem

~Kebz