Print Hello World In A Drupal 7x Module

Drupal 7x module development
Date: 
Mon, 06/02/2012

This tutorial shows a simple first steps of setting up a Druapl 7x module, if you have ever created Druapl 6x modules this is a useful start to show the differences in creating the files.

First you need to create a folder called helloworld in sites/all/modules/helloworld In here create a helloworld.info and .module file with the code below. Create a template file page-helloworld.tpl.php in your theme directory and add the template statement. You need to enable your module at the normal admin module page http://www.yourdomain.com/admin/build/modules . If you now go to the page http://www.youryourdomain.com/helloworld you will see the printed statement.

The template file for page-helloworld.tpl.php

<?php
print $content;
?>

The helloworld.info file

name = helloworld
description = Hello World module
package = custom 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';
    }
?>

(Note that as of Drupal 4.7, the ?> at the end of code files is purposely omitted. This includes for module and include files. Do not add this when copying.)

Your rating: None Average: 4 (1 vote)