Saturday, 12 December 2015

Learn to submit suggested changes to Drupal as patch files


Patch files are an integral part of any Project. In the process of development, few errors or bugs are always left behind even by the best team of developers such as Drupal. Patch files are never very large in size(Until they tackle non program data,such as multimedia). Although they are used in every field of the project, they have particularly the role of fixing security holes. Creating patch files is one of the best methods of contributing to a project. Lets walk through a summary on 'How to create Patch Files' .


This post is for people using Windows.

1. First of all I will recommend you to install Github(Its only recommended, you can also work on Drupal projects without it). GitHub is a web-based repository service. In general, it is a software which is used by team of developers to work on their projects simultaneously. To install Github for Windows(I am using Windows). Go to GitHub Desktop. Click on the download icon in the middle of the page. A downloader will be downloaded. Open the setup and it will prompt you to download the whole Github Desktop. Install it according to your comfort.
A typical GitHub start-up page.
Now open the GitHub Desktop, click on the small 'settings' icon on the top-right corner and select GitShell from the drop down menu which will appear. Login with your GitHub username and mail by typing the commands: 
git config --global.username USERNAME
git config EMAIL
Replace the above 'USERNAME' and 'EMAIL' with your own. Don't have one ? Go create one at Github.

2. You will need to have a local server up and running. A php, database manager and the server of course. For all these I used Xampp. It will install php, Mysql database and Apache server,it also has a ftp client(Filezilla). After installing Xampp go to its install directory and open the xampp-control.exe .

xampp-control.exe frontview
Click on the start button given in front of Apache and Mysql. Your server is now up and good to go.

*Pro tip-Install Xampp in C: rather than in C:\Program Files. This way it works better.
Close Skype from the Task Manager otherwise it will create clashes. 

3. Now we will clone Drupal Sandbox repository . For this open GitHub desktop which you downloaded in the first place. On the top-right corner there is a settings icon. Click on it and select 'Open in GitShell' a command line window will appear. Go to C:\xampp\htdocs using command prompt by typing  cd C:\xampp\htdocs . Create a folder 'sandbox' there by using the command md sandbox . Now we will clone the Drupal Sandbox directory here by giving the command: git clone --branch 8.x . Now a download will get started.(I cloned the repository in D:\Sandbox and then had to move it to C:\xampp\htdocs, so you should clone it in firsthand to C:\xampp\htdocs).

Sandbox Clone Downloaded.
4. After the download is completed open localhost/phpmyadmin in your browser window. On the front page Click on the database tab. Make sure that both Apache server and Mysql are running.
Database tab of phpmyadmin.
In the 'Create Database' field type the name as 'sandbox' and click on create. In my case I named it as 'bax'. I don't why I named it so but it happened by impulse. Although I would recommend you to add a password to your profile by clicking on the 'User Accounts Tab' but is not required.

5. Now we will install Drupal Sandbox locally on your pc. Make sure Apache server and Mysql are up. Go to localhost/sandbox/core/install.php. Opt for the standard installation. Follow the on-screen instructions and go with the default settings. In the 'setup database' tab enter username as 'root', database name as 'sandbox'(If you followed what I said), and leave the password field blank.(If you did not add a password to your profile or otherwise enter the password.)  
Database configuration
After the installation is complete your local Drupal Sandbox site should be up and running and should look like this.

*Note-My site name is Lord_of_codes_Sandbox

6. Now we will add some commands to the .gitconfig file to make your coding experience more colorful and interesting. Open Git Shell and enter the following commands:

git config --global color.status auto
git config --global color.branch auto 
git config --global color.interactive auto
git config color.diff auto

Configuring .gitconfig file

Issue ID : 1681300


Creating a branch for development:

1. First, lets rectify the issue that we have to clone. Open your browser and go to localhost/sandbox and click on Content(Present on left pane)>Add Content >Article (node/add/article) and scroll down below and click on URL path settings. Check out the help text. Did it mean anything to you ? Did you understand it? No na, the text is uninterpretable and quite misleading. This the problem we have to tackle by applying the patch.

The problem

2. Open GitHub Desktop. Click on the half-filled small rectangular in the top-left corner of the application.  Now click on the '+' icon which appears after the menu slides to the left and click on 'add' tab and enter the address C:\xampp\htdocs\sandbox. Finally click on 'add repository'.

Ignore the code in background.
3. Now again open Git Shell. You will be in your sandbox directory. If not 'cd' to it.

4. Check in which you currently are by typing : git branch . For creating a new branch where the patch development will take place, type: git branch 1681300-rewrite-description-for-URL-alias

5Checkout the branch you just created by typing git checkout 1681300-rewrite-description-for-url-alias. Type: git branch to confirm you are on the branch you just created.

Creating a branch for development.

6. Now open Notepad, click on File>Open.  The open dialog box will appear. Change the file type from .txt to 'All Files' from the drop down menu present right next to the file name field. Now, browse to C:\xampp\htdocs\sandbox\modules\path\path.module . Open the file. Use Ctrl+f to find this line :
Optionally specify an alternative URL by which this content can be accessed. For example, type "about" when writing an about page. Use a relative path and don't add a trailing slash or the URL alias won't work.
 and replace it with this line :
The alternative URL for this content. For example, type "about" when creating an about page.Use a relative path without a trailing slash.

Commiting changes to path.module

7. Lets check whether the issue is resolved or not. Refresh your browser page Again scroll to the part where the bug appeared. The 'help' text should now have changed to a more understandable phrase.
It makes sense now, isn't it?

8. Now its time to save the whole progress by committing the changes. To commit type git commit -am "1681300-Updated-description for URL on node form."
*Note - My node ID is 1681300 so I am using it as it is, you may use your own.

commiting changes
9. Now its time to create the patch file but first you have to comply the name of your patch with Drupal patch name standards. Which is this Description-NodeID-CommentNumber.patch.
For the comment number part you will manually have to visit that issue and find out the comment number your patch will be attached to. In my case the node ID is 1681300, Description is rewrite-description-for-url-alias and the comment number was supposed to be 29.
Hence my patch name : rewrite-description-for-url-alias-1681300-29.patch

creating patch
*The patch file submitted by me is available at :

10. Caution : Before uploading always test the patch file. To test the file type: git apply --check  rewrite-description-for-url-alias-1681300-29.patch . If nothing returns in reply from the Command Prompt, your patch successfully worked.

Applying patch
11. You successfully created a patch, now go upload it to the issue queue or share it with your friends or whatever you like.


First of all I would like to thank my mentor tadityar & Chandan Singh, the Google Code-In team and the whole Drupal Community. After completing this task I have learnt how to write and apply patches for Drupal Core issues and every contrib module or project, basically any element of the Drupal project. So go to search for issues requiring patch and provide them with it. You and I will now be able to contribute to Drupal in this specific category. 
All hail Drupal!

The world's most powerful CMS.
Thank You.

No comments:

Post a Comment