Thursday, July 31, 2008

Writing your first Drupal custom module

How to write a drupal module to meet your custom requirements?
here is a simple guide. This assumes that you already have a working installation of drupal 6.x.

There are lot of tutorials available out there. I do not want to add one more. I just want to provide the developer a jump start by omitting most of the advanced stuff.

Let us write a module which does nothing but print the famous "Hello World" in the content area (not in the side bar) and call it "helloworld" module.

Now for directories and conventions. In the drupal installation directory, you will find "modules" directory. This is the directory which contains modules officially packaged with drupal. Don't mess this directory. You will also find a "sites" directory where installation specific modules need to be put. Under this directory, you will find "all" directory. The modules under this directory will be available for all sites catered by that particular installation (drupal can cater to multiple sites using one installation). Let us use this "all" directory. Create a directory "modules" under this. The "modules" directory will hold the custom modules. Create a directory "helloworld" under "modules". This will be the directory for our module files.

For our module to work, we need two files under this directory. info file and module file. info file contains information (such as id, version, etc) about your module and module file contains the code.

Let us create info file first. Create a file called "" under helloworld directory. Then write the below lines into that file.

; $Id$ Helloworld
name = Hello World
description = A simple module which prints Hello World on the screen.
core = 6.x
Now a little bit of explanation. First line is the identifier for your module. You can add date, version etc to it. Let us keep it simple for the time being. Second line is a name which appears in the title of the content for your module. Third line is a small description which appears in admin section. Fourth line tells that our module is compatible with drupal 6.x.

Now for the module file. Create a file called helloworld.module in the same directory. This file contains php code. The coding convention is to open the file with no end tag. That is right, no end tag.

The content for the module files goes here.

function helloworld_perm()
return array('access helloworld content');
The above function tells drupal to create permission control in admin section for our module for accessing content. Without this, users (except admin) will not be able to access the contents of the module.

function helloworld_content()
$content="Hello, World!";
return $content;
The above function returns the content of the module. In our case, it is a simple string "Hello, World!".

function helloworld_menu()
$items = array();
$items['helloworld'] = array( 'title' => 'Hello World', 'page callback' => 'helloworld_content', 'access arguments' => array('access helloworld content'), 'type' => MENU_CALLBACK );

return $items;
This is the last function in the module file. This will add a callback which enables us to access our module contents using url like /index.php?q=helloworld or /helloworld.

Put all these 3 functions in the module file after
Now, login to drupal as admin. Go to modules under site building. At the end of the modules list, you should find your module. Enable the module by checking the checkbox and clicking "save configuration" button.

Now go to permissions under "User management". You should find a section "helloworld module". Check "authenticated user" checkbox against "access helloworld content". Configuration for our module is ready.

Logoff admin and login as a regular user. Access the URL /helloworld or /index.php?q=helloworld (depending on whether you have enabled smart url or not). You should see "Hello, World!" in the content pane.

About drupal custom modules

There are lot of CMSs (Content management system) out there, most popular currently being Drupal and Joomla.
There are lots of debates over which of these CMS is better. In my
opinion, each have their own strength and weaknesses. But when it comes
to power of customization, I choose Drupal over Joomla.

Ok, so we have a drupal powered website and we need some specific functionality which drupal does not provide out-of-box. So what do we do? Write our own module?

No! Before getting into development of custom module, I recommend searching in the modules contributed by users on drupal's website. Most of the times, you find one which suits your requirements, and sometimes you will be pleasantly surprised by the additional features it may have.

Why? What is the problem with me developing my own module?
Ok, here is why.
1. It saves a lot of time you put into development.
2. User contributed modules would have been tested by several people, issues detected and fixed. And hence will be more stable.
3. You will be able to get help/info on the module in the drupal forum.
4. To cut it short, no point reinventing the wheel.

Suppose you didn't find a suitable module or you just want to learn how to write a drupal module, check my other post here
Writing your first Drupal custom module.

Wednesday, July 30, 2008

PHP Code Beautifier

I had been searching for php code indenter/beautifier for long time. There are several online versions available. But I wanted a command line client. Finally I found one at It does a neat job and has lot of options. Windows and linux versions are available.

They seem to have 3 versions. From their website,

  • A GUI version which allow to process file visually.
  • A command line version which allow to be batched or integrated with other tools (CVS, SubVersion, IDE ...).
  • As an integrated tool of PHPEdit.

Here is the URL for phpCodeBeautifier.