Login

Creating A Simple Drupal Module -- Files and Directories

This this diary we are demonstrating how to create a simple Drupal module ( see introduction for more details) .

Our modules name is "My Color".

We will create one directory and three files:

  • Directory "my_color".
  • File "my_color.info".
  • File "my_color.module".
  • File "my_color.install".

 Let's start with directories.  Perform the following steps:

  • Locate the "sites/all" directory in your Drupal installation.
  • Create a sub-directory of "sites/all" called "modules". The result is "sites/all/modules".
  • Create a sub-directory of "sites/all/modules" called "my_color". The result is "sites/all/modules/my_color".

Let's create our files.  Perform the following steps:

Create a text file called "my_color.info". Save the file in in the "sites/all/modules/my_color" directory. Add the following content to the file "my_color.info":

name = My Color
description = "Allow folks to enter and display colors."
core = 6.x
project = "my_color"
datestamp = "126686725"

 Create a text file called "my_color.install". Save the file in the "sites/all/modules/my_color" directory. Add the following content to the file "my_color.install":

<?php
function my_color_install() {
    drupal_set_message(t('Beginning installation of My Color module'));
    switch($GLOBALS['db_type']) {
            case 'mysql':
            case 'mysqli':
                $table_created = db_query ("CREATE TABLE my_color (
                        id int NOT NULL AUTO_INCREMENT,
                        name  varchar(80) NOT NULL,
                        PRIMARY KEY(id))");
                 break;
           case 'pqsql':
                $table_created =db_query("CREATE TABLE my_color (
                        id int NOT NULL serial,
                        name varchar(80) NOT NULL,
                        PRIMARY KEY(id))");
                 break;
            default:
                drupal_set_message(t('Unsupported database'));
    }
    if ($table_created) {
      drupal_set_message(
      t('My Color installed database tables sucessfully'));
    }
    else {
      drupal_set_message(
      t('The installation of the My Color module failed .'),'error');
    }
}

 Create a text file called "my_color.module". Save the file in the "sites/all/modules/my_color" directory. Add the following content to the file "my_color.module"

<?php

 Please note!

We will add content to our "my_color.module" file in subsequent posts in this diary. The focus of this post is to get a the basic and essential module directory, file, names and locations completed.

When writing Drupal custom modules, do not use the closing "?>" in your file.


Summary

You should now have a new subdirectory which includes 3 new files:

  • Directory: "sites/all/modules/my_color"
  • File: "sites/all/modules/my_color/my_color.info"
  • File" "sites/all/modules/my_color/my_color.install"
  • File" "sites/all/modules/my_color/my_color.module"

Explanation:

"my_color.info" is used by Drupal admistration pages to describe the new custom module. "my_color.install" simply connects to your existing Drupal database and creates a new database table.

You can now login to your Drupal installation as with the admistrator login. Go to the Adminstrator/Modules. Enable "My Color" (by checking the box). Click on "Save Configuration".

Drupal should display a success message "My Color installed database tables sucessfully".


Verifying the base (database table) installation

It's always best to verify any work you are doing with a database, outside of your application code.

In this case, we added a new database table "my_color".

Lets go the command line, and check that our new table exists. Perform the following:

Go to the command line.

  • Log in to MySql "$ mysql -u root -p my_drupal_database" (Note! You will be prompted from the password).
  • At the mysql prompt, enter the following command: "explain my_color;"
  • If your table has been created sucessfully, you should see the following:
 
mysql> explain my_color;
 [Field][Type]   [Null][Key][Default][Extra]
 [id]   [int(11)] [NO] [PRI] [NULL] [auto_increment] 
 [name] [varchar(80)] [NO] [ ] [NULL] [] 

You can also just issue a simple select statement from the "mysql>" command line: "select * from my_color". The database will return no rows, however, you still know the table exists. 


Note! The complete module code is detailed in the next post titled "Create a Simple Drupal Module - Use the Form API -- Completing the Code"