In this post we will discuss on creating a custom block through a module in Drupal 6.  One question can come into your mind that we can create block from the administrative menu/site building/blocks. So, what is the need to create block from module.

The benefit of creating block from module is that, the module can be placed in any theme or in any other sites and once the module is enable the block will created automatically with its contents as we have configured in the module.

Some easy steps to create block through module :

Step 1 :

Create a folder (give a folder name e.g., mymodule) in the modules directory of your Drupal site.  The name which you are giving as a folder name will become your module name. To create a module there must have two files inside the module folder named as:

( a) mymodule.info (b) mymodule.module

mymodule.info file contains the general information of the module you created and mymodule.module file contains the main coding which creates the block.

# We are considering the module name as customblock and hence filenames are customblock.info and customblock.module

Step 2 :

Copy the code given below and paste it inside the customblock.info file.

; $Id: customblock.info,v 1.4 2009/02/18 22:02:46 dries Exp $
name = Custom Block
description = Creating Custom Block to demonstrate the block creation through module.
package = Diadem
version = VERSION

; Information added by drupal.org packaging script on 2008-10-08
version = “5.11”
project = “drupal”
datestamp = “1223496909”

This is the general information about the module and description field reflect the purpose of creating the module . Give the correct version which you are using for your drupal site. You need to change this information as per your requirement.

Step 3 :

Always remember that all the function name should start with the module name, e.g.  modulename_node_info(), modulename_perm(), etc. Copy the code given below and paste it inside the customblock.module file. You will observe that there has no php end tag at the end of the file. It is recommended in drupal that we donot need to end the php tag in the module pages.

<?php
/**
* Implementation of hook_node_info().
*/
function customblock_node_info() {
return array(
‘customblock’ => array(
‘name’ => t(‘Custom Block’),
‘module’ => ‘customblock’,
‘description’ => t(‘How to create a custom Block.’),
)
);
}

/**
* Access Permission of this module by hook_perm();
*/
function customblock_perm() {
return array(‘access customblock content’);
}

/**
* Implementation of hook_block().
*/
function customblock_block($op=’list’, $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == “list”) {
$block[0][“info”] = t(‘Custom Block’);
return $block;
}
else if($op == ‘view’)
{
$block_content=”;
$block_content.='<div style=”border:1px solid #D6D6D6; background-color:#F2F2F2; padding: 5px;”>’;

$block_content.='<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Why do we use it? <br />
<a href=”http://drupal.org/” target=”_blank”>Click Here</a> – Drupal.Org
</div>’;
$block_content.='</div>’;

$block[‘subject’] = ‘Custom Block’;
$block[‘content’] = $block_content;
return $block;
}
}

Explanation :

1)  customblock_node_info() : This is the implementation of the hook_node_info function defined in Drupal. This function contains the information about the module. E.g.: name which will show in the module listing page. Description – Some general information about the module and module – module name.

2) customblock_perm(): This is the implementation of hook_perm function defined in Drupal.

3) customblock_block() : This is the implementation of hook_block function defined in Drupal. This function is responsible to return the block content. You can see, we have defined a variable $block_content which contain the block with its contents and properties.

$block[‘subject’] variable contains name of the block created. Finally, we assigned the $block_content into the $block[‘content’] variable beacuse this varible is responsible to create the block.

step 4 :

Enable the customblock module from administrative menu/site building/modules.

mod

Custom Block Module

Once you enable the module a block named as Custom Block will be created . Configure the block from administrative menu/site building/blocks. Finally, place the block where you want to display in your drupal site.

Block
[ratings]