Lets build a simple module "Hello World"

What is helloworld module and why we need this? helloworld module is a simple Drupal module which prints the text "Hello, world". In every programming language, you can find a simple "Hello, World" program. This is the simplest programs possible in a computer language. In Drupal, we need such type of a module which prints the text "Hello World". So that all beginners who want to develop a module can follow this. So lets build a simple module helloWorld.

1. helloworld.info
What is .info file? This file contains all the meta information about the module. Through this file, your Drupal installation will know about this module. Before we start, please make sure that you have created a directory "helloworld" and put it in the modules directory. Now create a file helloworld.info, put the following code and save it under "helloworld" directory.

;$Id$;
name = Hello World
description = This is simple module that print out "Hello World"
core = 6.x

The above informations are the basic information that you must provide when creating a module.

2. helloworld.module
The module system of Drupal is based on the concept of hook. One can define hook as Drupal's internal event or callbacks. Hooks allow modules to interact with the rest of Drupal. In the helloWorld module, we will use help, perm, menu, block hook.

Before moving forward, please check the documentation on writing secure code and the Coding standards in drupal.org for more information.

Help Hook
With help hook (hook_help), we can provide module related information or any other help information.

/**
* Implementation of hook_help().
*/
function helloworld_help($path, $arg) {
    switch ($path) {
        case 'helloworld':
            return t('<p><strong>Here we can print all the help messages or other information.</strong></p>');
    }
}

Perm Hook
Perm hook (hook_perm) defines permission types of the module. Through this hook we can configure who can use this module. After defining the permission types, the configuration option will show in Permissions page under User Management of your Drupal site.

/**
* Implementation of hook_perm().
*/
function helloworld_perm() {
    return array('access helloworld');
}

Here we are creating a permission type "access helloworld", which we can use later in the module. Because the perm hook just specifies what permissions are available for this module. It does not define what the permission means.

Menu hook
Menu hook (hook_menu) define menu items and page callbacks. With this hook, we can register paths for page callbacks.

/**
* Implementation of hook_menu().
*/
function helloworld_menu() {
    $items = array();
    $items['helloworld'] = array(
        'title' => 'Hello World',
        'description' => 'Hello World!.',
        'page callback' => 'helloworld_all',
        'access arguments' => array('access helloworld'),
        'type' => MENU_NORMAL_ITEM
    );
    return $items;
}
/**
* Creating the page where we will print "Hello World"
*/
function helloworld_all() {
    return t("Hello, World!");
}

Block hook
With block hook (hook_block), we can create a block or set of blocks.

/**
* Implementation of hook_block().
*/
function helloworld_block($op = 'list', $delta = 0, $edit = array()) {
    if ($op == "list") {
        $block = array();
        $block[0]["info"] = t('helloWorld');
        return $block;
    }
    elseif ($op == 'view') {
        $block_content = '<p>This is an example block that prints "Hello World".</p>';
        $block['subject'] = 'Hello World';
        $block['content'] = $block_content;
        return $block;
    }
}

With the above block hook code, we are creating a block that prints some messages.

So, with all the above code, we have done with our module. You can download the files from the following attachment link. After installing the module, u can see a menu item as "Hello World" under the navigation menu. Also in the admin block page, u will see the helloWorld block.

AttachmentSize
helloworld.zip1 KB

There are no comments for "Lets build a simple module "Hello World"".

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.