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

An insight into the world of Drupal – Part 5

1687

Let’s build a module (Part 2)

 

From previous article, we have taken the first steps in writing a Drupal module. Before going any further let’s revisit the basic steps of creating a Drupal module.

1. Creating the module folder
2.Creating the .info file
3.Creating the .module file
4. Installing the module

Little bit more

Other than the absolute basics of writing a Drupal module given above, there are some other tidbits that become handy when working in real world. For example, in more occasions than not, Drupal modules have to interact with a database to manage its data. Therefore Drupal has provided mechanisms to easily execute such operations.

Interacting with Drupal DB

If a module required to create a table structure at the installation or setup variables in the database as placeholders (to store module settings.etc), Drupal allows the developer to implement this through .install scripts.

Revisiting last article example where we started creating our customized module named computer_store, we will be required to have at least one tables to manage computer part indexes. Therefore let’s initiate this table through our .install script.

 

1. Create a file named  computer_store.install within our previously created module folder computer_store.

      2. Add an install hook to the script as follows. Within this hook state the SQL script you want to run at the module installation.

 

<?php
// computer_store.install

function computer_store_install() {
switch ($GLOBALS['db_type']) {
case ‘mysql’:
case ‘mysqli’:

db_query(“CREATE TABLE {computer_store_index} (
store_index int unsigned NOT NULL AUTOINCREMENT default ’0′,
part_name varchar(32) NOT NULL default ”,
price float unsigned NOT NULL default ’0′,
PRIMARY KEY  (store_index)
) /*!40100 DEFAULT CHARACTER SET utf8 */;”);
break;

case ‘pgsql’:
db_query(“CREATE TABLE {computer_store_index} (
store_index serial CHECK (store_index >= 0),
part_name varchar(32) NOT NULL default ”,
price float(10) NOT NULL default ’0′ CHECK (price >= 0),
PRIMARY KEY  (store_index)
)”);
break;
}
}
?>

 

Explanation of the code:

 

At the installation of any Drupal module, if there exists an  install hook (computer_store_install) like in the above code, it’s called and code is executed. The PHP global variable ‘db_type’ ($GLOBALS['db_type']) keeps the database type of the Drupal instance and thus allows you to provide compatibility with a wide range of SQL database management systems such as MySQL and PostgreSQL.  Drupal’s db_query function (http://api.drupal.org/api/function/db_query) used above provides a high level interface to query databases so it can be used to query various SQL databases.

Other than creating tables, we can also set variables in the Drupal database to keep various module settings. For example to keep the maximum allowed number of computer parts in the store, we can set a variable named “maximum_store_parts” in the database. To do this add the following line to our previous computer_store_install hook.

variable_set(“maximum_store_parts”, 10000);

Removing the module

As well as providing a mechanism to create a table structure, there needs to be a way to remove created tables when the module is being removed. For this Drupal provides an  uninstall_hook. To remove the table we created in our install_hook when the module in uninstalled, add an uninstall_hook to our computer_store.install script.

 

function computer_store_uninstall() {
db_query(‘DROP TABLE {computer_store_index}’);
variable_del(‘maximum_store_parts‘);

 

cache_clear_all(‘*’, ‘cache’, TRUE);
}

 

Explanation of the code:

When the computer_store module is uninstalled from the Drupl backend, function computer_store_uninstall is called and we use the same db_query method to remove the previously created computer_store_index table from the Drupal database. Then the method variable_del is called to delete the variable maximum_store_parts from the database, which was set when the module is installed from the install hook. Then optionally we call cache_clear_all targeting Drupal ‘cache’ table to remove any stored cache in regards to the removed module.

Updating the module

In case we need to update our module table structure at a later time (usually some time after the module is released), we can add an update hook to our module and release a new version of our module so when users update their Drupal instances the module will also get updated.

Each update is placed in a modulename_update_x() function (where x is an incrementing integer) and updates should always increment by 1.

function computer_store_update_1() {
$items = array();
$items[] = update_sql(“ALTER TABLE {computer_store_index} ADD description”);
}

Note that if you add an update hook to your .install file you need to also update the install hook so that it contains the latest code. When a module is installed the first time, only the install hook will be fired and it will assume that is most current. Update hooks are only used when updating an existing installation.

More Support Materials

 

Writing .install files – http://drupal.org/node/51220

Hook install – http://api.drupal.org/api/function/hook_install/5

Hook uninstall – http://api.drupal.org/api/function/hook_uninstall/5

Hook update – http://api.drupal.org/?q=api/function/hook_update_N/5

Hook cache clear – http://api.drupal.org/api/function/cache_clear_all/5

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