A short tutorial on developing a simple hello world module that prints Hello World on the screen.

1. Create a folder for your module

Create a folder in your modules directory called helloworld ie sites/all/modules/helloworld/. It is good practice to put all custom modules in sites/all/modules/ folder and not the modules/ folder. The modules folder is meant for core modules any “third party” or custom modules should be in sites/all/modules/.

So now that you’ve created the folder sites/all/modules/helloworld/

2. Create the *.info file

Create a file called modulename.info in this case helloworld.info

; $Id$
name = Hello World Module
description = My very first Drupal Module
core = 6.x

This lets Drupal know about your module

3. Create the *.module file

Create a file called modulename.module in this case helloworld.module

We need to let Drupal know what path the module will be accessible from. We do this by using the hook_menu() hook. To implement  hooks follow the following convention: modulename_hookname(). So for our module we must create a function called helloworld_menu(). More info here.

function helloworld_menu(){
$items = array();
$items['helloworld'] = array(
'title' => t('Welcome to the Hello World Module'),
'page callback' => 'helloworld_page',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
  );
  return $items;
}

Break Down

We created $items[‘helloworld’] array item. This lets drupal know about the path we want to use for our module.

title: this is what gets printed at the top of the page. The page title.
page callback: This is the function that gets called when the url is accesed.
page arguments: must be array of arguments that get parsed to the call back function, not required.
access arguments: array of access permissions in this case any role with ‘access content’ permissions can access the page.
type: specifies the menu type check hook_menu() for more details the Drupal hook.

There are more available options than these. The Drupal API is your friend. Use it.

So in a nutshell, when Drupal hits the http://mysite.com/helloworld url it will look for a call back function called helloworld_page(). Simple Enough. Now lets create the helloworld_page() function.

function helloworld_page($argument) {
   return "Hello World!";
}

So your helloworld.module file will contain:

<?php
function helloworld_menu(){
$items = array();
$items['helloworld'] = array(
'title' => t('Welcome to the Hello World Module'),
'page callback' => 'helloworld_page',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
  );
  return $items;
}
function helloworld_page($argument) {
    return "Hello World! Arguments parsed was " . $argument;
}

Note how i did not close the <?php tag. It is considered good practice to no close files that only contain php. Now go to admin>build>modules. Your module should be on the list. Activate it and type in http://mysite.com/helloworld to test it out. Goodluck

I’ll doing a secong part to this tutorial where i will show you how to create an input form and save user the input to the database.

You can download the complete drupal module.

Once done with this easy bit Check out Part 2 of the tutorial

Enjoy

Thanks
Tali

function simple_hello_world_menu(){
  $items = array();
  $items['simple-hello-world'] = array(
    'title' => t('Simple Hello World!'),
    'page callback' => 'simple_hello_world_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}