Home Classroom Drupal An insight into the world of Drupal – Part 4

An insight into the world of Drupal – Part 4

1193

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

We have computer storage and in need of a web based store backend to keep track of items stored and sold. So we will be developing a Drupal module for this task with the intention of scaling it to add a shopping cart in the future.

 

 

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

 If we are not working on a mulitsite configuration, we have to create a folder with the name of our intended module name in ‘/sites/all/modules’ (as of Drupal 5.x). But if you are in a multisite configuration the folder should be created in your intended site configuration (e.g. sites/yoursite.com/modules) and for the sake of simplicity let’s work on a single site (without any multisite complexities) for our first Drupal module creation.

 

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.

 

Step 3 -  Creating the .module file

This is the main script of our module. So it goes without saying that we will be working with it a lot.

 

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

 Goto Administrator->Site Building->Modules section and our new module should be listed there  in a category called “E-commerce” ! Even though we still have to build an install script, for the heck of it you can just install our new module and check how it goes.

 

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

In this week article we have touched the basics of art of module creation in Drupal. In next week let’s go on adding more details such as a database table stricture to our module to bring it to some form of usefulness.

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

Comments

comments

Laknath Buddhika is an old Royalist and currently doing undergraduate studies at Faculty of IT, University of Moratuwa. He has worked on various fields of IT for more than a decade and is currently employed at Vesess Pvt Ltd for more than 2 years. He is an avid enthusiast of Open Source and has participated in Google Summer of code programme twice(2007,2008) representing Open Source organizations Gnome, Eclipse and also a member and a module contributor for Drupal community. He has a keen interest in the PHP language and PHP based CMSs such as Drupal, WordPress and Frameworks like CakePHP and CodeIgniter. He is also employed at DMS Pvt Ltd as a part-time PHP lecturer and wishes to spread PHP and the idea of Open Source throughout the Sri Lankan society.

NO COMMENTS

Leave a Reply