We have developed a skeleton for our module in the previous article. Let’s start adding the functionality to our module. Remember that the name of our module is “example” and it is stored under “<Drupal Installed Location>/sites/all/modules/example
“ directory.
So, what are we going to do with this module? Let’s define what we are going to do with our module.
I want to keep things simple. Just our module will display a “Welcome!” message with a logged-in user name whenever a user logged into our Drupal-based website. This is the objective of our module. So, simple right?
Drupal 7 is purely based on the hooking concept. For example, at the time of loading all the modules, Drupal will ask each module, whether the particular hook is implemented or not. If it implements, Drupal will load and run that particular hook.
Drupal hook_user_login
hook
Step 1. Our first task is to implement one of such hooks, hook_user_login. The syntax of this hook function is:
function hook_user_login(&$edit, $account)
Where $edit is the array of form values submitted by the user. $account is the user object.
We have to implement this hook function. So, our hook function is going to be example_user_login.
Notice that we replaced “hook” with module name “example”. Yes, this is the standard Drupal uses. So, all of our module hooks will start with the name of the module “example”.
Drupal 7 will call this function whenever a user logs in to our Drupal-based website.
So, what are we going to put in this hook function?
Calling Drupal’s drupal_set_message
function
Step 2. Now we know where to place our code. We are going to place our code into the hook function example_user_login
. Add the following code:
drupal_set_message(t('Welcome, ' . $account->name . '!'), 'status');
We are calling another Drupal function, drupal_set_message
from inside our hook function example_user_login. drupal_set_message sets a message to display to the user. We are setting a welcome message; so, whenever users log in to our Drupal-based website this welcome message will be displayed.
If we see the syntax of drupal_set_message:
drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE);
drupal_set_message has all optional parameters. $message is the message we want to set. $type is the type of the message; whether it is a status message or warning message or an error message. $repeat is a flag to tell whether to repeat the message or not; that means if the message is already set, it won’t set the same message of the same type next time.
Then what about $account->name?
$account is user object, we are getting it from example_user_login. It has a name property, which gives the logged-in user name. So, we used the same here.
Drupal .module
implementation
Step 3. Now we are ready with our code. But in which file do we need to put this code? We have to place our code into the “example.module” file; which we have created in our previous article.
Once we place the code, the file “example.module” looks like below:
<?php /** * Implementation of hook_user_login() */ function example_user_login(&$edit, $account) { drupal_set_message(t('Welcome, ' . $account->name . '!.'), 'status'); }
Observe that, we have started our module file with <?php PHP tag and no need to worry by closing with ?>. Drupal automatically places the PHP close tag. So, we have not added a close tag in our module file.
Step 4. We have our example module in handy. Now we need to enable our example module.
Go to the Modules page in the Drupal administration panel and enable our Example module.
Step 5. Log out from our Drupal-based website and log in again. And now you observe; once you log in you will see the message “Welcome, <log in user>!” on your screen.
We have successfully created out the simple module in Drupal 7.
**
3 thoughts on “Drupal 7 – Creating a module (Part – 2)”