RSS Feed

Create your first simple Drupal 7 module

June 28, 2010 by admin

Create your first Drupal 7 module with the following steps.

  1. Create a folder called helloworld in sites/all/modules/custom
  2. Create a helloworld.info file
  3. Create a template file page-helloworld.tpl.php in your theme directory
  4. Enable your module at http://domain.com/admin/build/modules
  5. Visit http://domain.com/helloworld

This belongs into your helloworld.info file

; $Id$
 
name = helloworld
description = Reinholds Hello World module
package = Reinholds modules
core = 7.x
 
files[] = helloworld.module

The helloworld.module file

<?php
	function helloworld_menu(){
	  $items = array();
 
	  $items['helloworld'] = array(
	    'title'            => t('Hello world'),
	    'page callback'    => 'helloworld_output',
	    'access arguments' => array('access content'),
	  );
 
	  return $items;
	}
 
	/*
	* Display output
	*/
	function helloworld_output() {
	  header('Content-type: text/plain; charset=UTF-8');
	  header('Content-Disposition: inline');
	  return 'helloworld';
	}
?>

The theme template file page-helloworld.tpl.php

<?php
print $content;
?>

36 Comments »

  1. its very interesting topic, , really nice & briefly explained about drupal 7 module, thank you for time spending for us..

  2. Everything is pretty much the same like in Drupal 6.

  3. ugam says:

    Where i can put helloworld.module
    thanks

  4. sahayarex says:

    very simple and nice… good work. thank you.

  5. Jatinder Bhambri says:

    Thx too much bro for help ,,,, i m a beginner in drupal ,, nd i starts with drupal 7 from last 3 dayz ,, can you plzz provide me more documentation regarding custom module creation ,, nd theam integration ,, if u have any pdf or any other mail compatible format then plzz mail me at jatinder.data.mca@gmail.com ,,,

    waiting for ur reply bro ,,,

  6. Rahul says:

    Hey i am not getting output using this.
    can you please help me.
    thanks in advance…

  7. dah prince says:

    wow! this is perfect.am now good to go.thanks

  8. Elijah says:

    This isn’t working:

    “Access denied
    You are not authorized to access this page.”

    Any idea what is causing this?

    • besso says:

      Elijah, did u solve ‘Access denied’ ?

      • besso says:

        Solved.

        I had some error in hook_menu()
        ‘page callback’ => ‘say_helloworld’, // array(‘access helloworld’), // <== was 'access content' value

        i know some banned restriction for path value.
        then i believe most case in our code T.T.. as it.

  9. Gajendra Chauhan says:

    very simple n easy………..
    Thanks

  10. neeraj says:

    Thanks a lot.
    It’s very simple and easy to apply. A good first step to understand and create complex modules.

  11. svm says:

    This works nicely, except that page-helloworld.tpl.php is ignored. When placed in the theme/whatever/templates folder, I can’t get it to be recognized, even after clearing the cache. Any ideas?

  12. hi dear,
    where to put following contents..

    thanks
    Balwant Bishnoi

  13. Ravi Kanojiya says:

    Thanks

  14. Hemant says:

    Hello Reinholds,

    Very short & simple. Thanks & appreciate you work for community.

    It would be very good if you explain code you mention in script, that’s helps a lot to beginner.

    By the way, good work & thanks from community.

    Hemant

  15. gingic says:

    I have run the example and works fine, but I want to have it displayed as block in block list and it doesn’t seems visible. How can I have it displayed as a block?

  16. Hi Reinholds,

    Very short & simple. Thanks & appreciate you work for community.

  17. adnan says:

    Simple and elegant. I was searching exactly the starting point of drupal 7 module developement and here I got this Simple and effective post about it :)

    Thanks

  18. And now. How can I show it in drupal?

    thnks.

  19. ben koonse says:

    sick write up bro, nice and clean, you sir are a scholar and a gentleman

  20. agurchand says:

    I’m novice in drupal. This really helped me. Thanks!

  21. ducnguyen says:

    To run it, you only need to enter yourdomain/helloworld

  22. Matin says:

    Very helpfull for begginner module developer as me.
    If we see output domain/helloworld

  23. napoleonite says:

    Brilliant! Helped me a lot!

  24. ranga says:

    Thank u very much…its very nice for drupal developers for understand coding…..

  25. JT says:

    If you don’t have Clean URLs enabled then you have to access this module at: example.com/?q=helloworld

    Otherwise you will get a 404 error at example.com/helloworld

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">