How To Create A New Block In Drupal 6
One of the most common questions that new drupal users come across is finding out how to create a new block.
Blocks are sections of a page that can be placed in any one of the regions supported by the theme that is currently active. A block usually gives some special features such as a log in form, a poll, a list of links to a section of your page or something similar. Some special modules add new blocks that you can place in your website.
Why Would You Add A Block?
You may want to add a new block for adding some HTML code for your such as for an advertisement block or a website hit counter. The block may need to have some text or HTML code that should be displayed in all (or selected) pages of your website.
You may even add a block to execute some PHP code in your web page. In this example I will show you how to add a block that displays the current time and date. Although I am using PHP code, you can even use HTML or even plain text.
How Would You Add A Block?
There are two methods to create a custom block for drupal. This article will discuss both methods. The first method can be used by anyone. The second method is better suited to you if you have to distribute the code to someone else.
There are two methods to create a custom block for drupal. This article will discuss both methods. The first method can be used by anyone. The second method is better suited to you if you have to distribute the code to someone else.
Important Note: If you are going to add some PHP code in the block, then you must enable the PHP input type. In drupal 6 this is done by enabling the PHP Filter core module. In drupal 5 this is done by going to Administer > Site Configuration > Input Formats and adding PHP Input type.

Step 1: Go To The Administration Panel > Blocks and click on the Add Block link (tab).

Step 2. Fill in the form along with the necessary PHP code. And click on the ‘Save Block’ button.

Advantage : The advantage of this method is that it is very simple. It can be done quickly and easily by anyone. Even a non-programmer can do it in a matter of minutes.
Disadvantage: For non-programmers this is a minor disadvantage – that you cannot distribute the block as a small piece of code that another person can place in their modules folder, enable it and arrange the new block on their website. For developers this method means they cannot distribute the functions of the block without giving long instructions on how to enable it (which is usually error-prone).
The Developer’s Method: Meant for creating a block for distribution
This method is ideal if you want to create special functionality that goes with your theme or just generally a new block for any drupal website.
In this method we will create a new module which will add a “Time And Date” module to the Administer > Site Building > Blocks section.
1. Create a new directory under sites/all/modules called timeanddate.
2. Under timeanddate directory, create a file called timeanddate.info and enter the following code in it as it is:
; $Id$ name = timeanddate version = 1.0 description =This module adds an advertisement block. This is to be used with the authorist theme core = 6.x PHP = 4.3
2. Under the same directory, create another file called timeanddate.module.
Firstly we have let drupal know that we want to create a new block. We do this by defining a function called _block(). Modulename here is timeanddate, so our function will be:
<?php
function timeanddate_block($op='list',$delta=array(),$edit=array())
{
switch ($op)
{
case 'list':
$blocks[0]['info'] = t('Time And Date');
return $blocks;
break;
}
}
Save it and go to Adminsiter > Site Building > Blocks. Defining this function in the timeandmodule.module file will add a new block named ‘Time And Date’ to your Administer > Site Building > Blocks section. Don’t try to add this to a region yet. You will not see a new region if you do so.
function timeanddate_block($op='list',$delta=array(),$edit=array())
{
switch ($op)
{
case 'list':
$blocks[0]['info'] = t('Time And Date');
return $blocks;
break;
case 'view':
ob_start();
//start block content
?>
enter your plain text or html here.
<?php
//end block content
$content = ob_get_clean();
$blocks['subject'] = t('Time And Date');
$blocks['content'] = $content;
break;
}
}
Enter your HTML code in place of “enter your plain text or html here”. If you are using PHP code to generate some output to the browser, replace the code that is in between ‘ob_start();’ and ‘$content = ob_get_clean();
In our example, we will be using php code, so our function, with the addtion of “echo date(‘gi:i d D m Y”);” which prints the current date and time, becomes this:
function timeanddate_block($op='list',$delta=array(),$edit=array())
{
switch ($op)
{
case 'list':
$blocks[0]['info'] = t('Time And Date');
return $blocks;
break;
case 'view':
ob_start();
echo date('gi:i d D m Y');
$content = ob_get_clean();
$blocks['subject'] = t('Time And Date');
$blocks['content'] = $content;
break;
}
}
That’s it. Now we have a block that shows the time. This module can be distributed to other website owners as well.