How to create a block module?

"Hello world" is a very common practice for anyone want to gain a basic understanding of any new programming language, system, CMS etc.

This article is intent to just show a very basic coverage of how to create a basic Drupal block module.

Drupal module is an essentially a hook calling to Drupal hooks. There are many hooks and you can find out more about all Drupal hooks at api.drupal.org.

But for this article, you will need to have a folder in modules call "hello" and in this folder, you should have at least two files: hello.info and hello.module. You can have another file like README.txt, LICENSE.txt, hello.install, etc.

File: hello.info
Note: This file will provide the basic information about our little hello block module


; $Id: Hello Exp $
name = Hello Module
description = Hello world in Drupal Module.
core = 6.x
package = Hello

File: hello.module
Note: This file will contain the actual code that you call to Drupal hook

<?php
// $Id: hello module Exp $

// keep the module name consistent throughout
global $moduleName;
$moduleName = "hello";

// call menu hook to create an administration menu to create content for your hello module
function hello_menu() {
global $moduleName;
$items = array();
$items['admin/settings/'.$moduleName] = array(
'title' => ucfirst($moduleName),
'description' => 'Set '.$moduleName.' code',
'page callback' => 'drupal_get_form',
'page arguments' => array($moduleName.'_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}

// call admin hook to let you administrate the content of this hello block
function hello_admin() {
global $moduleName;
$form = array();
$form[$moduleName.'_show_title'] = array(
'#type' => 'checkbox',
'#title' => t('Show title'),
'#default_value' => variable_get($moduleName.'_show_title', '1'),
'#required' => TRUE,
);
$form[$moduleName.'_number'] = array(
'#type' => 'textfield',
'#title' => t('How many blocks'),
'#default_value' => variable_get($moduleName.'_number', '1'),
'#maxlength' => 3,
'#required' => TRUE,
);
$n = (int)variable_get($moduleName.'_number',1);
for( $i=1;$i<$n+1;$i++ ) {
($i<2) ? $setreq = TRUE : $setreq = FALSE;
$form[$moduleName.'_'.$i] = array(
'#type' => 'textarea',
'#title' => t(ucfirst($moduleName).' Block '.($i)),
'#default_value' => variable_get($moduleName.'_'.$i, ''),
'#maxlength' => 800,
'#required' => $setreq,
);
}
return system_settings_form($form);
}

// call block hook and show in Drupal block you assign
function hello_block($op = 'list', $delta = 0) {
global $moduleName;
$block = array();
if($op == 'list') {
for($i=1;$i

You can copy and paste this code and create in the respective file in your modules folder and have working Drupal block module. You can use this just for playing with hello concept or you can use this to create something more practical like a simple banner block that you can assign the banner to show in right, left, header or footer of your Drupal site. Just copy and paste your banner ads code in the Textarea and assign the position then it will show.

I hope you will find this little tutorial helpful!

Thanks

Forums: