12 March 2011

This beginners tutorial shows how to create a durpal module. We will set up a module, create a menu item and a "Hello World" page.

Folder for our module

Create new folder in sites/all/modules directory. My module name is Hello World so I'll name the folder to hello_world.

Info file

Create a new file called hello_world.info.
Source code viewer
  1. ; $Id: hello_world.info
  2. name = Hello World
  3. description = "Hello World page."
  4. core = 7.x
  5. files[] = hello_world.module
Programming Language: YAML
If you use drupal 6 not 7 then remove files variable and change core to 6.x.

PHP file

You can access the page from http://example.com/admin/hello_world. Don't forget to enable the module and if you make changes in function names then you have to clear drupals cache.
Source code viewer
  1. <?php
  2.  
  3. /**
  4. * Implementation of hook_menu().
  5. */
  6. function hello_world_menu()
  7. {
  8. $items['admin/hello_world'] = array( // path to your module output
  9. 'title' => 'Hello World', // page title
  10. 'page callback' => 'hello_world_page', // function that is called when you land to this url
  11. 'access callback' => TRUE, // always and everybody can access this url
  12. 'type' => MENU_CALLBACK, // no menu item will be made, simple callback is made
  13. );
  14. return $items;
  15. }
  16.  
  17. /**
  18. * Page callback.
  19. */
  20. function hello_world_page()
  21. {
  22. /**
  23.   If you return you get you content in Drupal if you echo you get your results in plain html.
  24.   This way you can output xml or what ever content you might need
  25.   */
  26. return 'Hello, World!';
  27. }
Programming Language: PHP