Different frontpage according to roles in Drupal

If you are building a Drupal application it is possible you will want anonymous users to see a welcome page that explains how to use it and invites people to create an account. Once you've convinced users to sign up it is not nice to keep showing them the same welcome message.

There is a very simple solution if you want to have a different frontpage for logged in and anonymous users in Drupal 7. I guess if you look for it you'll find a module that will do that for you. Using third part modules is a good idea, but it takes away some of your flexibility. Our solution will use a custom module ― if you don't know how to do it yet, please check out this tutorial. I actually recommend you to always have a custom module installed so you can write simple and handy functions in it quickly.

Our custom module will implement hook_menu using as callback a function that redirects the user to the appropriate according to you criteria. If you are not familiar with using hooks check this out.

Here is an example:


 /*
  * Implementation of hook_menu
  */
 function custom_menu(){
         $menu = array();
         $menu['frontpage']=array(
                 'page callback'=>'custom_frontpage',
                 'access arguments'=>array('access content'),
                 'type'=>MENU_CALLBACK,
         );
         return $menu;
 }
 function custom_frontpage(){
         if(user_is_logged_in()){
                 drupal_goto('panel');
         }else{
                 drupal_goto('welcome');
         }
 }

After writing your module's functions you'll need to rebuild your menu cache. This is important. It will not work otherwise.

How to rebuild menu cache: You may do it by editing some menu or updating your site in /update.php.

Finally, access your site admin pages in the addres /admin/config/system/site-information and set the site's front page to 'frontpage' as used in our example. This will do the trick.

Understanding a bit more

Hook
A hook is a function Drupal calls from every module when the time is right. There are lots of predefined hooks in Drupal and you can get things done quickly thanks to this: you implement a hook simply by writing a function named your module name followed by an underscore and the name of the hook. My custom module name is custom. To implement the hook_menu I simply write a function called custom_menu.
Hook menu
The hook hook_menu sets the urls Drupal knows. This means that any urls matched by the patterns specified in any hook menu will be answered by Drupal using the parameter defined by the same function. A hook_menu implementation must return an array containing another array for each url to be created. In our example we created the array $menu with a single key-value pair inside: a key 'frontpage' with a array as value, containing the page callback, the access arguments and the type of the new page. The page callback is the function to be called when someone access this url. Such function must return a string that will be rendered as the content of the page. The access arguments parameter specifies which permission one must have in order to see this content. The type parameter specifies what sort of menu this item is. For our purposes we can stick with MENU_CALLBACK.
Redirecting users
The drupal_goto function redirects the user to another url. We used Drupal internal addresses here (not relative addresses). In order to check if the user is logged in Drupal 7 uses the function user_is_logged_in which is exactly what we needed.
comments powered by Disqus