An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Saturday, February 28, 2009

Step by Step Guide - Creating New Modules in Drupal

No comments :
Keywords: Creating a New Module in Drupal, New Module, New Module in Drupal, Drupal New Module, Custom Module, Custom Module in Drupal

Creating a New Custom Module in DRUPAL

Step 1: Create a folder in drupal_root/sites/all/modules/MODULE_NAME
For this you may have to create modules folder in drupal_root/sites/all/ if it doesn't exists
Ex: drupal_root/sites/all/modules/foo

Step 2: Create an information file called "modulename.info" to tell drupal about your module. The file name shall be "modulename.info" and it should be placed in the "drupal_root/sites/all/modules/MODULE_NAME" folder.
Ex: foo.info

Step 3: The information file should have the general information about the new module.
Ex: foo.info shall have the following information
name = FOO
description = A description of FOO
core = 6.x
package = Administration

Step 4: Create a module functional file called "modulename.module". The file name shall be "modulename.module" and it should be placed in the "drupal_root/sites/all/modules/MODULE_NAME" folder.
Ex: foo.module

Step 5: Now you have to define the contents, functionality for the new module. That shall be done by creating appropriate functions
Ex: foo.module shall have the following information if you are creating a block module

//File: foo.module
//Code Starts
//http://kathyravan.blogspot.com

function foo_block($op='list', $delta=0) {

if( $op == "list" ){

$block[0]["info"] = t("Foo");
return $block;

}else{

$block['subject'] = 'Foo';
$menuString = '
    ';

    $block['content'] = $menuString;
    return $block;

    }

    }
    //Code Ends
    ?>

    This step will display the menu links.

    Step 6: If you want to create any tables while drupal enables your module, you can use hook_enable(), hook_disable() functions in foo.install file.
    Ex: A sample .install file (foo.install here) is given below:

    //File: foo.module
    //Code Starts
    //http://kathyravan.blogspot.com

    function foo_enable( ){

     drupal_install_schema('helpdesk');

    }

    function foo_schema( ){

    $schema['table_name'] = array(

    'description' => t('Description of the Table.'),
    'fields' => array(
    'field_id' => array(
    'description' => t('FIELD ID.'),
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE),
    'field_status' => array(
    'description' => t('FieldStatus'),
    'type' => 'int'),

    'field_date' => array(
    'description' => t('FieldDate'),
    'type' => 'int'),

    ),
    'indexes' => array(
    'ifield_id' => array('field_id'),
    ),
    'unique keys' => array(
    'ufield_id' => array('field_id'),
    ),
    'primary key' => array('field_id'),

    );

    return $schema;
    }

    ?>

    Note: "foo_enable" function will be called after drupal enabled your module. "drupal_install_schema" will install the tables defined in the module. Same wise "foo_disable" function will be called after drupal disabled your module.
    For more information, pls visit


    Step 7: Go to "Administer › Site building > Modules", install your module. Check if the table are installed properly.

    Step 8: Go to "Administer › Site building > Blocks", select the region of display for your module.

    Step 9: Navigate the module.

    For more information, pls visit:


    List of Drupal Hook Functions

    Keywords: Creating a New Module in Drupal, New Module, New Module in Drupal, Drupal New Module, Custom Module, Custom Module in Drupal

    Featured Blog Topics:


    No comments :