I want to find a basic "Hello World" module to start working on Drupal 7. Can anyone help please?

Thank you for your time.

Comments

WorldFallz’s picture

lorinpda’s picture

Hi,
I am assuming you have successfully installed Drupal 7. Here is a bare bones "Hello World" module.

File: /sites/all/modules//mymodule/mymodule.module

Note! I created a subdirectory called "mymodule".

<?php

function mymodule_menu() {
    $items = array();

    $items['my_module/hello_world'] = array(
        'title' => 'Hello World Test',
        'page callback' => 'say_hello_world',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
     );

    return $items;
}

function say_hello_world() {
    $vars = array();
    $vars['type']="ul";
    $vars['title'] = "";
    $vars['attributes']=array("");
    $vars['items'][0]="This is a simple proof of concept module";

    return theme_item_list($vars);
}

File: /sites/all/modules/mymodule/mymodule.info

name = mymodule
description = test proof of concept module
package = public-action
version = VERSION
core = 7.x
files[] = mymodule.module


; Information added by drupal.org packaging script on 2010-03-21
version = "7.0-alpha3"
project = "drupal"
datestamp = "1269192313"

Install the module through the Admin Modules interface (dashboard). The new module will probably be listed at the very bottom of the admin/modules page.

Use the Admin Performance function and clear all cache. That will make sure Drupal recognizes the map to your function (Drupal jargon for request mapper is "Menu").

To invoke hello world go to : http://mysite/my_module/hello_world

Hope that helps.

parogne’s picture

I do have D7 installed, I am having a little bit of trouble with getting this module to display but I will keep tweaking and get it worked out. Do I need to have clean URL activated to get it to go to the /my_module/hello_world address?

lorinpda’s picture

No, you don't need to turn on any clean URL's. Drupal refers to url mapping (request dispatcher) as a "Menu". The function "mymodule_menu()" maps the url "my_module/hello_word" to the say_hello_world() function.

Drupal also uses "module" names as a pseudo name space. Therefore make sure that you create the subdirectories and file names exactly as described in my example.

You must clear the "cache" in order for Drupal to recognize the url map.

Is the say_hello_world() function being called? is there a specific reason why you need a "hello world" module for Drupal 7 and not Drupal 6?

You might want to check a series of blog posts I put together for beginning Drupal developers. Takes you step by step on how to create a simple module. The hello world module in this thread contains less functionality, however this may help (note! blog posts for Drupal 6).

What the sample module will do:

http://public-action.org/content/drupal-beginners-programming-create-sim...

Creating the files and sub-directories:

http://public-action.org/content/drupal-beginners-programming-creating-s...

Finally the completed code:

http://public-action.org/content/drupal-beginners-programming-create-sim...

Hope that helps.

Niraj@PHP’s picture

Thanx a lot...but i am not getting the term "$items['my_module/hello_world']", when i run the module i write in the url as
localhost/my_module/hello_world...how this work i cant understand.

PawelR’s picture

hook_menu puts 'my_module/hello_world' string in to menu_router database table along with corresponding function to call when this URL is accessed.

in .htaccess (placed in Drupal's root folder) there is definition which puts everything after host in to $_GET['q'] variable and calls index.php file. for example:

http://yourdomain.com/my_module/hello_world

becomes:

http://yourdomain.com/index.php?q=my_module/hello_world

index.php performs thousands of operations called bootstrap process, during this process Drupal uses $_GET['q'] variable to get name of the function to call from menu_router table and calls your function.

Niraj@PHP’s picture

Thanx for reply me.

But when i am changing the code by any thing as like "$items['anything']" instead of "$items['my_module/hello_world']",
then also my module is runing by url localhost/my_module/hello_world. how is happing it i am not getting ?

my code is and in this code line number 3 i am not getting
-------------------------------------------------------

function mymodule_menu() {
$items = array();
$items['my_module/hello_world'] = array(
'title' => 'Hello World Test',
'page callback' => 'say_hello_world',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);

return $items;
}

jordojuice’s picture

When you change the path 'mymodule/hello_world' the menu router needs to be rebuilt to pick up the new URL. Until you do that Drupal is still relying on the old path. To do this you can reinstall the module or better you should get the devel module. It will provide a block with a bunch of useful links for development. You can use the rebuild menus link to rebuild the menu router. Use $items['admin/something'] to make an item appear in the administration menu. In other words, normal menu items items prepended with admin/ go in the admin menu.

jbdrupal’s picture

Hey, perfect for beginners wanting to do a hello world module to learn.... Try the free videos at https://buildamodule.com. Click videos at the top. Some are for free. The ones you want just happen to be free teaser videos. Click Drupal 7 Development Core Concepts. There is a wonderful set of videos to explain a hello world module and line by line explanation for the .info and .module files.

Rizwan Siddiquee’s picture

the best example how to create Drupal module programatically.

check it >> http://www.letmecrazy.com/drupal8-how-create-custom-block-programatically/