Drupal theming provides you with the ability to completely override the output that Drupal gives you by default. This tutorial will explain about making new themes to override the default theme functionality of Drupal.
Create a new directory in the sites/all/themes folder. This is the place where all user defined themes should go. Name the new directory as ‘mytheme’ which will be the name of your new theme.
Creating the .info file:
Create a .info file with the name mytheme.info. This file defines metadata for the theme, including the name of the theme, a description, its Drupal core version compatibility, and which theme engine it uses. Style sheets, JavaScripts, Block regions are also defined here. Enter the following contents now:
; $Id$
name = Mytheme
description = Just a new theme.
version = VERSION
core = 6.x
engine = phptemplate
You will notice that at the top of files you download from Drupal. org there is always a line commented out that starts with $Id:, with a name and some date information that follows after. This is a special ID tag used by the version control system used by Drupal.org (CVS) to maintain all of the core and contributed code. Your custom files don’t need this line on them but it doesn’t hurt to have it there either. The empty version of this string is simply $Id$.
Now if you go to Administer->Site Building->Themes you will be able to see your new theme in the list. Now if you select this theme you will see a disorganized output. This is the default output provided by different modules. The main page is displayed by system/page.tpl. You can see this by using the Themer info function of the Devel module.
In order to organize the page output you need to override page.tpl (theme_page()). Lets copy the page.tpl file from bluemarine theme and paste it in our theme, then empty cache (using Devel), and reload the page. Now the page is organized into different regions. If you copy the style.css file also the the page would appear styled.
Changing CSS:
Let’s start by adding a border around content items to help them stand out on the page. Using Firebug in Firefox we can find out the exact element that styles the node, which is the .node at line 232 of the styles.css. Change it as follows:
.node {
margin: .5em 0 2em; /* LTR */
border: 1px dashed #ccc;
padding: .5em;
}
Your page will now appear with dashed lines around the nodes. You can also override other CSS files, but then you will have to include it in the .info file of the theme like this example :stylesheets[all][] = system-menus.css. Simmiliarly if you are adding javascript files you need to add it to the .info file like this: scripts[] = script.js
Modifying a Template file : (*.tpl.php files)
Now let’s look at customizing the page.tpl.php file. This is the file that defines the general HTML structure of the entire page. The theme engine and enabled modules make a number of variables available to this file, and then these variables are simply output using PHP print statements. Let’s change the structure of our theme by moving the breadcrumb output up into the header region of the page.
Using the Themer info of Devel module you can find out how the breadcrumb is beign output. The parent file is page.tpl.php, so our breadcrumb is being printed from page.tpl.php. The function used to make the $breadcrumb variable is theme_breadcrumb().

Open up the page.tpl.php file in your ‘mytheme’ folder and go to about line 29, where the $header variable is printed. Down at line 42 is the $breadcrumb variable we want to move.
Now let’s modify the file to output the breadcrumbs immediately after the header, by removing line 42 and restoring it as a modification to line 30 as follows:
<tr>
<td colspan="2"><div><?php print $header ?><?php print $breadcrumb ?></div></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" id="content">
In a similiar way you can change the appearance of other items like node,block.. using node.tpl.php,block.tpl.php.
Template.php:
The last, crucial piece of Drupal theming is the ability to completely override the output that Drupal gives you by default and use your own custom markup instead. You may find that while the template files give you a lot of control, you can’t really do much about the HTML that you are given inside those variables (like $header,$breadcrumbs). You can do all the HTML editing around them that you want, but how do you crack into the variables themselves. This is where template.php comes into picture. Using template.php file we can override or add variables and also override theme functions.
Overriding/Adding Variables:
All variables that go out to a template file are first passed through a special kind of function, called a preprocess function. You can add your own preprocess function to your template.php file and get the last shot at the variables before they head out to the template. To keep things tidy, you can use one preprocess function per template; for example, you can create a mytheme_preprocess_page function to affect variables to be used in your page.tpl.php file. With this function, you can define, or redefine, any variable.
For example you can define a variable called $random_number as follows:
function mytheme_preprocess_page(&$vars) {
$vars['random_number'] = rand(1, 100);
}
Then (after clearing the Drupal cache), you can print out that variable out in your page.tpl.php file just like any other variable:
<?php print $random_number; ?>
You use the exact same procedure to override an existing variable. When you assign a variable name that is the same as the one Drupal is already using, yours will take precedence.For example to change $submitted from ‘Submitted by…’ to ‘Posted on…’ :
function mytheme_preprocess_node(&$vars) {
$vars['submitted'] = t('Posted on ') .format_date($vars['node']->created, 'custom', 'F j, Y');
}
Here is documentation listing all variables available in page.tpl.php, node.tpl.php, and block.tpl.php.
Overriding Theme functions: (theme hooks)
Much of Drupal’s HTML output is easily accessible in template files. But what about something like the page’s “breadcrumb trail,” Unfortunately, there’s no breadcrumb.tpl.php file. In this situation, you need to dig a little bit deeper, to the place where remaining markup in Drupal is initially defined: ‘theme functions’.
Theme functions are regular PHP functions located in Drupal’s code whose names begin with theme_ (present in theme.inc). Every element on the page that is not in a template file is run through a theme function, such as the theme_breadcrumb() function. The function basically just takes an array of HTML links and uses PHP’s implode() function to concatenate the values using the » character, wrapping this in a <div> with the class of “breadcrumb.”
function theme_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
return '<div>'. implode(' » ', $breadcrumb) .'</div>';
}}
In its current state, this function will print out the breadcrumb trail on a page, such as Home » Administer » Site building » Themes. But what if we decide that we’d rather the breadcrumb be printed as Home::Administer::Site building::Themes, with doublecolons instead? The proper way to handle overriding theme functions is by simply copying the function into your template.php file and naming it according to Drupal’s naming conventions so that it’s recognized. Then you can modify it however you like.
function mytheme_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
return '<div>'. implode(' :: ', $breadcrumb) .'</div>';
}}