
An insight into the world of Drupal
05/29/2009 10:13 am By Laknath Buddhika | Articles: 5
Part 4 – Let's build a module (Part 1)
When we take Drupal developments as a whole, it can be separated into two main parts; Developing Drupal themes and module developments. Within this article we'll be focusing on how to get on with Drupal module development process.
Drupal Modules
Drupal modules are the building blocks and the workhorse of a Drupal site. Since at the first installation Drupal only gives just a barely running site, usually all other customizations and special workload is carried out by various modules.
A Drupal site can have three kinds of modules.
1. Core modules that ship with Drupal and are approved by the core developers and the community
2. Contributed modules written by the Drupal community and shared under the same GNU Public License (GPL)
3. Custom modules created by the developer of the website.
From the Drupal modules download page you can find hundreds of readily available 'Community contributed modules' that can be downloaded and installed on a whim. But on the other hand, if there are any custom tasks that you need to be carried out through the site, you will have to develop it yourself and install it same as any other contributed module. Also sometimes you'll have to modify existing contributed modules to suit your project requirements so you have to take a look at the code and hack it. On the other hand, if there's any wrong with the existing code of a contributed code you can just fix it and send a patch to the patch queue of that module project. So for all of the above mentioned, it's absolutely necessary to have a knowledge of the Drupal module development process and how to read a module.
So let's get in to the action.
The Goal
Getting Started
Within every module, it's required to have at least two files.
1) Info file – a file with the extension of “.info” to provide information about the module to Drupal site
2) Module file – a file with the extension of “.module” as the main execution point of the module
Other than above two, there can be other files in a Drupal module that work mainly to break tasks of the module to small sections.
1) Install file – a file with the extension of “.install” that provides that database creation instructions when a module is installed and uninstalled
2) Include files – although not specifically designed for a Drupal implementation, as a general rule include files with the extension of .inc are used to form small libraries that can be later included in to a main script
So we have to get some of these files in our hands for our first module.
Step 1 - Create the module folder
Let's name our first module as computer_store and create a folder with the name computer_store in '/sites/all/modules' of your Drupal deployment.
Step 2 – Creating the .info file
Now we have to let Drupal know about our new module through a .info file. So let's create it as our first file in our module directory.
Create a file named computer_store with the extension of .info (computer_store.info) in your computer_store directory and enter following.
<code>
name = computer_store
description = "A module to manages our computer store details"
package = "E-Commerce"
</code>
For a simple briefing of the lines in our .info file, name gives out the name of our module and the description field tells a brief description of our module that will be displayed in the module listing page in Drupal. Package is an optional field that tells Drupal in what category should our new module appear in.
Create a file named computer_store with the extension of .module (computer_store.module) in your computer_store directory and enter following.
<code>
<?php
/**
* Display help and module information
* @param section which section of the site we're displaying help
* @return help text for section
*/
function computer_store_help($section='') {
$output = '';
switch ($section) {
case "admin/help#computer_store":
$output = '<p>'. t("Manages our computer storage and other details."). '</p>';
break;
}
return $output;
}
?>
</code>
Even though the above code is just the tip of the ice-burg it will give what we need – a start. What we have done basically in the above script is to add a hook_help to our module.
As I mentioned in an earlier article, hooks are implemented in Drupal to extend some known functionalities to modules and Drupal can call them when the suitable action is fired. In this case, the hook_help will be called when a user goes to Administrator->Help->Computer_store as displayed in P4: Figure 2 and P4: Figure 3 ( More on hook_help check http://api.drupal.org/api/function/hook_help/5). But before doing that we have to first install our module.
Step 4 - Installing the module
If it installs correctly, you should be able to see it with the checkbox next to “computer_store” ticked in the Drupal modules page, as shown in the P4: Figure 1.

P4: Figure 2 – computer_store module listed in Drupal help page

P4: Figure 3 – computer_store help description
Next week
You can contact the author via laknath [at] vesess.com should you have any problems from implementing the above.
More Support Materials
Drupal API references - http://api.drupal.org/api/
Drupal coding standards - http://drupal.org/coding-standards
Writing secure code - http://drupal.org/writing-secure-code



Drupal is a good CMS
I am a student of web development. I want to learn Drupal at advanced level. The basics i already known so please anybody can suggest me an online source to get help.
Kate
article directory, articleworlddb
great Information
I can give the same information about the I.T certification
Post new comment