Pages

Thursday, September 13, 2012

Create your First Custom Drupal Module Step by Step - Hello World!


The focus of this chapter is module creation. In the last chapter we learned about Drupal's Installations.Now its time to start our coding with our first modules.

Here are some of the important topics that we will cover in this course:

Basics

Starting a new module
Creating .info files to provide Drupal with module information
Creating .module files to store Drupal code
Adding new blocks using the Block Subsystem
Using common Drupal functions


Advacned

Formatting code according to the Drupal coding standards
Writing an automated test for Drupal

We are going to divide this task of building a new module into the three parts:
Create a new module folder structures
Work with the Block 
Write automated tests using the SimpleTest framework included in Drupal

Step 1

Type of Modules 






Folder Strctures of each Modules



Step 2: 

Creating the module directory

Now that we know that our modules should go in /sites/default/modules, we can create a new module there.The directory should be named with the machine-readable name of the module. 

Create a module directory in /sites/default/modules, and then place at least two files inside the directory: 
a .info (pronounced "dot-info") file
b .module ("dot-module") file.

We are going to name our first module with the Hello world, since it is our first module. Thus, we will create a new directory, /sites/default/modules/simple_hello_world, and then create a simple_hello_world.info file and a simple_hello_world.module file as like below




.info file

Check with below codes and write same code on your simple_hello_world.info file


; $Id$

name = simple_hello_world
description = This is my first sample module
package = phpcmsjobs modules
core = 7.x

files[] = simple_hello_world.module

The first line is a standard. Every .info file should begin with ;$Id$. What is this? It is the placeholder for the version control system to store information about the file.

The first two are required in every .info file. 

name : name Directive is used to declare what the module's human-readable name is. 

Description : Provides a one or two-sentence description of what this module provides or is used for. Among other places, this information is displayed on the module configuration section of the administration pages as like below




Package : The third item, package, identifies which family (package) of modules this module is related to.
Core : Declares which main-line version of Drupal is required by the module.

Step 3: 

Creating a module file

The .module file is a PHP file that conventionally contains all of the major hook implementations for a module.



function simple_hello_world_menu(){

 $items = array();

 $items['simple_hello_world'] = array(
   'title'            => t('Hello world! This is my First Module'),
   'page callback'    => 'simple_hello_world_output',
   'access arguments' => array('access content'),
 );

 return $items;
}

/*
* Display output
*/
function simple_hello_world_output() {
 header('Content-type: text/plain; charset=UTF-8');
 header('Content-Disposition: inline');
 return 'simple_hello_world';
  }

        ?>
  
  The theme template file page-helloworld.tpl.php

print $content; ?>
  
Step 4: 

  To invoke hello world go to : http://example.com/simple_hello_world

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

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

Congratulations, Now you have successfully created the First Drupal Custom Modules. 

Additionally we will see How to create a module with Simple Form? very shortly..

Please share your feedback here.....


Related Posts

1 comment:

  1. Custom CMS are often the best option as it smoothes the transformation of mundane website into customized website. It has user-friendly features that help in updating the website easily.

    ReplyDelete