Search This Blog

May 19, 2011

Contact Form Module for Drupal - Part 1

There are several contact form module that built for Drupal. However, I would like to introduce to you on how to create your own Contact Form module for Drupal. In this moment, I assume that you at least read and understand how Drupal Module works. You can learn the basic steps in creating module in their Module’s Developer Guide.

In building Drupal Module you need to go under the following ladder of learning. You can refer to the following links in moving forward.

It is quite need time to master all of the module stuff but all you need to do is to dedicate and a lot of coding practice.
Let me show one basic example of Drupal Module. This is a contact form module.
In creating contact form module, you will need more or less the following file.
  • - info file
  • contact.install – the installation file
  • contact.module – module file
  • contact -block.tpl.php – the tpl file
  • contact.css – a css file
  • contact.js – js file

Let’s go one by one.

The file contain the following

name (Required) – the display name of your module.
description (Required) – A short description of your module
core (Required) – it refers to the drupal version where module compatible with
dependencies (Optional) – dependcies module which required by your module to be create before some one can successfully install it.
package (Optional) – it is simply group you modules with the same package name in drupal backend. Good example is the name of your company. In our example, our .info file looks like this.

; $Id:,v 1.0 2009/07/01 05:50:57 t
name = NVR Contact
description = NVR Contact.
package = NVR Contact
core = 6.x

; Information added by packaging script on 2009-07-1
version = "6.x"
project = "nvr"
datestamp = "1240859105"

The second file is the contact.install. As the name speaks itself, this file is the one which create the database schema for our module.

There are three actions that we do to our contact.install file. The first is, the installation of contact table in your drupal database.
function contact_install() {
  db_query("UPDATE {system} SET weight = 0 WHERE name = 'contact'");
contact_install is drupal hooks that install table in your database. To understand what is hooks and know other hooks, visit this page.

Ok, the next hook that we need to do is the hook schema. This hook assign fields to our newly created table.
function contact_schema() { 
  $schema['contact'] = array( 
    'fields' => array( 
      'id'      => array('type' => 'serial', 'not null' => TRUE), 
      'name'    => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
      'email'   => array('type' => 'varchar','length' => 50, 'not null' => TRUE),  
    'primary key' => array('id'), 
  return $schema; 

And the last, if there were hook install there should be a hook uninstall.
 * Implementation of hook_uninstall().
function nvr_uninstall() {
  // Remove tables.
  db_query("DELETE FROM {system} WHERE name = 'contact'");
  db_query("DELETE FROM {menu_links} WHERE module = 'contact'");

The 3 combined hooks complete the contact.install file.

Continue here...