CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Drupal 7 – Creating a module (Part – 6)

We are almost done with our module. Just we have to implement the functionality when the module is installed or uninstalled. Drupal 7 loads the functionality from “.install” file whenever the module is installed or uninstalled.

We need to write hook functions “hook_install” and “hook_uninstall” in “.install” file. So, Drupal 7 will call these functions when the module is installed or uninstalled. If the module is installed Drupal 7 will call “hook_install” function. The same way when the module is uninstalled Drupal 7 will call “hook_uninstall” function.

What functionality we have to put in these two hook functions? Our module is responsible for displaying the “Hello!” message whenever the user logged in into their Drupal based websites. In our previous articles, we have created our module specific configuration settings page to allow the administrator to add/modify configuration settings. These settings will be stored into the database. When the user install our module, we need to add necessary entries into the database. We have remove these entries when the user uninstall our module. Because the settings are module specific, we should remove them from the database to avoid using unnecessary space in the database.

Lets start implementing the functionality.

Step 1. Login into your website’s Control Panel.

Step 2. Open the File Manager and create a file with the name “example.install” under “/sites/all/modules/example” directory. This is the location we have created “example.info”, “example.module” and “example.admin.inc” files.

Step 3. Now we need to add the functionality when the user install our module. We have to add “example_install” function into the file “example.install” file. Remember that we are using “example_user_roles” field to store our module’s configuration settings. When the administrator initially opens our module’s configuration settings page; there will be no entries in the database and through our code we have instructed Drupal to select ‘administrator’ as the default value through “example_admin_settings” function. But when the administrator saves the configurations; Drupal will automatically creates “example_user_roles” variable in “variable” table. So, we no need to add code to add this variable information into the database at the time of installing the module. Hence our “example_install” function is going to be empty.

/**
 * Implementation of hook_install()
*/
function example_install()
{
   // Do nothing
}

Step 4. But we need to remove this variable “example_user_roles” from the database when the user uninstall the module. We need to add this functionality in “example_uninstall” hook function.

Drupal 7 provides “variable_del” API to delete the variable from “variable” database table. The syntax of the function is:

function variable_del($name)

Where $name is the name of the variable you want to delete from the “variable” database table.

Now putting all together here is the code which should go into “example.install” file.

<?php

/**
 * Implementation of hook_install()
*/
function example_install()
{
   // Do nothing
}

/**
 * Implementation of hook_uninstall()
*/
function example_uninstall()
{   
   variable_del('example_user_roles');  
}

Observe that variable “example_user_roles” from “variable” database table is deleted only when we uninstall the module; not when we disable the module.

Now our full fledged Drupal 7 module is successfully completed. To distribute the module we can compress the “example” folder into “example.tar.gz”. At the time of installing the module we can select “example.tar.gz” and Drupal will install the module.

Enjoy Reading…

**

Drupal 7 – Creating a module (Part – 6)

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top