Drupal 7: Creating a module (Part – 5)

We have successfully created a configuration settings form for our Example module. In our previous article we have instructed Drupal to create a form for us. Most of the form functionality is prepared for us by Drupal.

Now when we submit our form, Drupal will call “example_admin_settings_submit” function. In this article we are going to implement this function. Perfect! Now what things we need to put inside “example_admin_settings_submit” function.?

Do you remember we have used “variable_get” function, to get the field value from the database? Drupal provides “variable_get” and “variable_set” functions to retrieve/store field values from/to the database. But why we need these functions to use? The purpose is, our module will display “Example!” message when the user login to the website. We are restricting this to particular user role(s) only in Example module’s configuration settings form. When site administrator save the changes made in configuration settings screen, we need to save those changes into the database. We have to implement this functionality in “example_admin_settings_submit” function.

Saved data will be retrieved from database using “variable_get” function and it will display on the form. This functionality we have already implemented while creating the form structure. So, no need to worry about this part now.

What information we need to store in database? Our form is displaying user role(s). Each user role is associated with a checkbox. Site administrator can select these checkboxes (single or multiple checkboxes) and submit the form. We need to store information about what user role(s) selected into the database. Based on this data, we will display the “Welcome!” message to the users who has into the particular user role(s). For example: if the site administrator selects “authenticated users” checkbox and unselects “administrator” checkbox; “Welcome!” message should be displayed to every logged in user except administrators.

The code for “example_admin_settings_submit” function looks like below: We have to add this function in “example.admin.inc” file.

/**
 * Implementation of example_admin_settings_submit()
*/
function example_admin_settings_submit($form, $form_state)
{
   // Do nothing
}

Why no code was added in above function? Is it by mistake? Fortunately, the functionality of storing data to the database is handled by Drupal API system_. So, we no need to explicitly store the data into the database. Our job became much easier.

What things left for us? Everything Drupal has done for us! But we also have to do something.

While displaying the “Welcome!” message to logged in users, we have to check whether the user is part of the selected user role(s) or not. If the logged in user is part of selected user role(s), then show the “Welcome!” message other wise Keep quite.

For this, we need to change our “hook_user_login” function. We have already implemented this function in our previous article; we have to modify this.

Our function will looks like below: Remember that we have added this function in “example.module” file.

/**
 * Implementation of hook_user_login()
*/
function example_user_login(&$edit, $account)
{
   $roles = variable_get('example_user_roles');

   $user_roles = array();
   foreach ( $account->roles as $key => $value )
      $user_roles[$key] = $key;

   $roles_intersect = array_intersect($roles, $user_roles);

   if ( isset($roles_intersect) && count($roles_intersect) != 0 )
      drupal_set_message(t('Welcome, ' . $account->name . '!.'), 'status');
}

From above code, we are checking the logged in user’s roles with the roles selected for Example module through configuration settings. When the administrator saves the configuration settings for Example module; the role ids will be stored as an array of key, value pairs (“role id = > role id”). For example: if role id “2” is selected and role id “3” is not selected, in database Drupal will store them as an array “2 => 2, 3 => 0”. That is the reason, we have generated $user_roles array as “Key => Key” pairs from logged in user roles. So that we can compare this with the roles stored in the database for Example module.

Save these changes into the file and modify the configuration settings for “Example” module and test whether “Welcome!” message is appearing for only the users who are has selected role(s).

Now we have extended our “Example” module by adding configuration settings form. I know, now you are feeling developing a Drupal module is not easy!

Are we done with module development? Yes. We are. Now we need to do few more additional things.

For example if we don’t want to use the module and we disable it; what would be the values it stores inside the database? They are still in the database. So, we need to remove them. When? We need to remove them when the module is disabled.

Usually we have to provide the functionality to the modules to do some activities when module is enabled and module is disabled.

We will discuss these in our next article.

Keep reading…

**

Leave a Reply