Saturday, May 5, 2012

Creation of Hello World module in Drupal 6


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 modulesfolder 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
?
1
2
3
4
; $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 thehook_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 .
?
1
2
3
4
5
6
7
8
9
10
11
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.

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.
?
1
2
3
function helloworld_page($argument) {
   return "Hello World!";
}
So your helloworld.module file will contain:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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;
}

How to easily create a custom module for Drupal 6?


Let's break this down into a few easy steps.
  1. Know where custom modules go, not in /modules but in /sites/all/modules
  2. Create a new folder there, e.g., new.
  3. Open a text editor and create a file named new.info
  4. In new.info add the following lines:
    ; $Id
    name = New
    description = A new module for testing
    package = Other
    core = 6.x
  5. Create another file named new.module
  6. In new.module add the following lines:
    <?php
    /**
    * Implementation of hook_block().
    * Adds a block that has new text.
    */
    function new_block($op = 'list', $delta = 0, $edit = array()) {
      switch ($op) {
        case 'list':
          $blocks[0]['info'] = t('New Module');
          return $blocks;
        case 'view':
          $block[0]['subject'] = t('New Module');
          $block[0]['content'] = t('Hi from your custom block and your first new module');
           return $block[$delta];
      }
    }
  7. Make sure both files are saved and then in Drupal go to Site Building -> Modules. Look under the other category and you should see your new module listed. Just check the box for it and submit.
  8. You now have a new module which creates a custom block, you can go to Site Building -> Blocks and you should see your new module block. You can select to enable it and add it your pages. 
  9. The possibilities with modules are nearly endless, I recommend you pick up Pro Drupal Development  for more instructions.