Following are the steps to create module in Drupal 6.

Step 1:

  • Goto sites/all/ in you drupal root folder and create a folder with the name modules.
  • Now select a name of the module you want to create and create a folder in sites/all/modules with the name you select.
  • For Example we select a name onthisdate. Now we created folder as sites/all/modules/onthisdate.

Step 2:

  • Create a file with module name and extension .info in sites/all/modules/onthisdate/ i.e sites/all/modules/onthisdate/onthisdate.info in our example.
  • Write basic information in it as follows and save,
  • name = On this date
    description = A block module that lists links to content created one week ago.
    core = 6.x
  • name, description and core information are the compulsory requirements for.info files of modules.
  • There are a couple of extra options that may appear in the .info file, one of which are module dependencies. If a module requires another module to be enabled, list each module (filename) required in the following syntax:
  • dependencies[] = taxonomy
  • dependencies[] = comment
  • If a module has a package string, on the admin/build/modules page it will be listed with other modules with the same category. If a package string is not assigned, it will simply be listed under ‘Other’. Not assigning a package for your module is perfectly ok; in general packages are best used for modules that are distributed together or are meant to be used together. If in doubt, leave this field blank.
  • package = “Your arbitrary grouping string”
  • Suggested examples of appropriate items for the package field:
    • Audio
    • Bot
    • CCK
    • Chat
    • E-Commerce
    • Event
    • Feed Parser
    • Organic groups
    • Station
    • Video
    • Views
    • Voting (if it uses/requires VotingAPI)

Step 3:

  • Create a file with module name and extension .module in sites/all/modules/onthisdate/ i.e sites/all/modules/onthisdate/onthisdate.module in our example.
  • Important note: It is not just a convention that the short name is used for both the module’s file name and as a function prefix. When you implement Drupal “hooks”, Drupal will only recognize your hook implementation functions if they have the same function name prefix as the name of the module file.
  • omit the closing ?> tag.
  • All functions in your module that will be used by Drupal are named {modulename}_{hook}, where “hook” is a pre-defined function name suffix. Drupal will call these functions to get specific data, so having these well-defined names means Drupal knows where to look.
  • To implement any hook in Drupal, replace “hook” in the hook name with your module’s short name, and create a function in the .module file with that name. So, to implement hook_help() in our example module, we create a function called onthisdate_help() in the onthisdate.module file:
  • <?php
    /**
    * Display help and module information
    * @param path which path of the site we’re displaying help
    * @param arg array that holds the current path as would be returned from arg() function
    * @return help text for the path
    */
    function onthisdate_help($path, $arg) {
    $output = ”;  //declare your output variable
    switch ($path) {
    case “admin/help#onthisdate”:
    $output = ‘<p>’.  t(“Displays links to nodes created on this date”) .’</p>’;
    break;
    }
    return $output;
    } // function onthisdate_help

Your module will therefore always begin with the following 2 files:

  • [your_module_name].info
  • [your_module_name].module

Step 4:

  • Now Defining Permissions for the module.
  • The next function to write is the permissions function, implementing Drupal’s hook_perm(). This is where you will define the names of the permissions of your module — it doesn’t grant permission or define what the permissions mean, it just specifies what permissions are available for this module. As with the help hook from the previous example, we implement hook_perm() by creating a function called onthisdate_perm() in the onthisdate.module file. This function just needs to return a list of the permission names this module will use; once you have defined permissions in the hook_perm() implementation in a module, an administrator can define which roles have those permissions on the Administer » User management » Permissions page.For example, in order to create a single permission called “access onthisdate content”, we would create the following function:
  • /**
    * Valid permissions for this module
    * @return array An array of valid permissions for the onthisdate module
    */
    function onthisdate_perm() {
    return array(‘access onthisdate content’);
    } // function onthisdate_perm()
  • For this tutorial, start with just the one permission. We’ll later expand to using additional permissions.

Step 5:

  • Now we are implementing functionality of our module.
  • Modules are created to do all sorts of things: some modules create blocks (abbreviated content that often appears on the right or left side of the page), others create special content types (for full page content – such as the content you are reading right now), others track back-end information, and some do all of the above. You may hear the phrases “Block modules” used to describe modules that primarily create block content (such as the menu module) or “Node modules” used to describe modules that primarily generate full page content (such as the blog and forum modules). At this stage, this module is a “block module”, because it generates a block.Later on we will show how to generate page content, which is very similar.The module will define a block that will eventually display the most recent blog and forum posts. The hook for creating blocks is called hook_block()To implement any hook in Drupal, replace “hook” in the hook name with your module’s short name, and create a function in the .module file with that name. So, we implement this hook by creating a function called onthisdate_block() in the onthisdate.module file.
  • /**
    * Implementation of hook_block().
    * @param string $op one of “list”, “view”, “save” and “configure”
    * @param integer $delta code to identify the block
    * @param array $edit only for “save” operation
    */
    function onthisdate_block($op = ‘list’, $delta = 0, $edit = array()) {// set up an empty array which will contain the block contents
    $block = array();switch ($op) {
    case “list”:
    // Generate listing of blocks from this module, for the admin/block page
    $block[0]["info"] = t(‘On This Date’);
    break;

    case “view”:
    break;

    case “save”:
    break;

    case “configure”:
    break;
    }

    return $block;
    } // function onthisdate_block

Step 6:

  • Now we are generating the block content.
  • The next step in this tutorial is to generate the content of the block. This will involve accessing the Drupal database. Our goal is to get a list of content (stored as “nodes” in the database) created a week ago. Specifically, we want the content created between midnight and 11:59pm on the day one week ago. When a node is first created, the time of creation is stored in the database. We’ll use this database field to find our data.To tell Drupal what content we want in the block, we use the ‘view’ operation of hook_block(). So, we’ll need to add some code to our previously-defined onthisdate_block()function.
  • function onthisdate_block($op=’list’, $delta=0, $edit = array()) {
    // set up the block
    $block = array();switch ($op) {
    case “list”:
    // Generate listing of blocks from this module, for the admin/block page
    $block[0]["info"] = t(“On This Date”);
    break;case “view”:
    // Generate content for blocks from this module
    $block_content = “”;

    //Get today’s date
    $today = getdate();

    //Get timestamp from one week ago
    $start_time = mktime(0,0,0, $today["mon"], ($today["mday"] -7), $today["year"]);

    //Limit timespan to one day
    $end_time = $start_time + 86400; //60*60*24 = seconds/day

    //Get nodes in range
    $query = “SELECT nid, title, created FROM ” .
    “{node} WHERE created >= ‘%d’ ” .
    ” AND created <= ‘%d’”;

    $query_result = db_query($query, $start_time, $end_time);

    //Set Block content
    while ($links = db_fetch_object($query_result)) {
    $block_content .= l($links->title, “node/”.$links->nid);
    $block_content .= “<br />”;
    }

    //Setup the block
    $block["subject"] = t(“On This Date”);

    //Check that content isn’t empty
    if ($block_content == “”) {
    $block_content = t(“Sorry No Content”);
    }

    $block["content"] = $block_content;
    break;

    case “save”:
    break;

    case “configure”:
    break;
    }

    return $block;
    }  // end onthisdate_block

Step 7:

  • Now we Installing, Enabling and Testing the module.
  • Log in as your site administrator, and navigate to the modules administration page to get an alphabetical list of modules. In the menus: Administer » Site building » Modules, or via URL:
    • http://example.com/admin/build/modules
    • http://example.com/?q=admin/build/modules

    When you scroll down, you’ll see the onthisdate module listed with the description next to it, in the “Other” section. Enable the module by selecting the checkbox and save your configuration.

  • The purpose of this module is to display a block, but just enabling the module doesn’t make the block display. You need to go to the blocks administration page (Admin >> Site building >> Blocks or paste admin/build/block) and enable it.Enable the block by selecting a region in the drop-down list for the ‘On This Date’ block and save your blocks. Be sure to adjust the location (left/right) if you are using a theme that limits where blocks are displayed. After saving the block region setting, you might also want to change the configuration (by clicking on the “configure” link) so that the block is only displayed on certain pages of your site or is only visible to certain roles. One thing to be aware of is that if you are logged into your site as User 1 (the original account created when you installed Drupal), you don’t necessarily have a role besides “authenticated user”.
  • To see the block, navigate to another page (it’s hard to see them on the Blocks page itself); if you chose to have the block visible only on certain pages, be sure to navigate to one of those pages. Note that our example module is configured to display only posts from the day exactly one week ago, and, if there are no results, to display the message “Sorry No Content”.For testing purposes, you may want to create some content (or edit some existing content) and adjust the “Authored on:” date to be a week ago, if your site lacks content from that particular date.