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 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 .
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 ;
}
|