Custom User Experience Based on User Roles

ArticleFebruary 18, 2013

Suppose you have a fantastic new Drupal site freshly crafted for your company. Your main focus has been creating a web presence that offers your anonymous visitors a way to access loads of information about who you are, what you do, what you offer and so on. Now, however, not only do you want to cater your existence to anonymous visitors, but you’d like to build a way for logged-in users, or even specific user roles, to have a slightly different experience than those who are just looking for the basic rundown of your services.

You’re off to a great start to do so. Drupal offers quite a few modules that allow you to alter the user experience. This includes opening up access to previously private paths (or presenting an entire new navigation setup altogether), moving content around, redirecting roles to specific front pages and even completely switching out your theme.

Creating a New User Role in Drupal

You have your administrative and anonymous users ready to go (perhaps a few authenticated users that will access certain parts of the Drupal UI for blog posting or updating page content), all of which can be easily controlled through your permissions settings. For this example, we will be working with an authenticated user role that will be called affiliate.  Set up your new user role (People > Permissions > Roles) and choose all permissions that apply.

Now you’re ready to begin customizing the UX for your new affiliate role. An affiliate user comes to your site for the first time and sets up their user account.  When they complete all of the required fields for becoming an affiliate, they will log in to your site and see that a few things have changed compared to what they had viewed as an anonymous user.

First, there’s the homepage. It’s still the same home URL from before, but now, there’s a few extra blocks of content that have opened up. There’s even a new secondary navigation menu for accessing previously hidden content. There are quite a few ways to achieve this, but one of the most straightforward methods that I’ve come across is through using the context module.

Using Context in Drupal

Under Structure > Context, you can control the content being displayed for a particular user role.  Add a new context and under conditions, set the user role to affiliate. Under reactions, you have quite a few options to choose from, but for the sake of this example, we will choose ‘block’ and then add new content to the appropriate regions. Once you’re satisfied with the new additions, save your context and try logging in as an affiliate to have a look at the changes.

Drupal context menu

Now you’ve given some thought to it, you probably want to completely change the landing page layout and what’s available to your affiliate once they are logged in. How can you present alternative primary navigation items, new content, and even a new layout? There are a few ways you can go about setting this up.  You can start by creating a new node (Content > Add Content > Content Type). Then go back into context and under conditions add a path to the node URL, for example affiliate-landing-page or node/12 (The node number being that of your new node ID).  After that, you can begin adding all the necessary content that will appear on this page under reactions.

Creating Rules for Site Redirects

Now you’ll want to setup a proper redirect for when the affiliate enters your site. A great module for delegating site redirects is the Rules module. You can learn more about Rules here.

Under Configuration > Workflow > Rules, you will create a rule to redirect your affiliate once they have logged in by building a new event, conditions for this event and an action that will take place when these conditions are met.

1. Add event user has logged in.
2. Add condition user has role(s) and choose the appropriate role.  In this case, we will pick affiliate.
3. Add action page redirect and add the URL for your new page.  Let’s use the URL of your new node.

The drupal rules backend menu

Now the affiliate has logged in and has been redirected to the brand new page you have created using context and rules. You’ve created tons of new content, specific for this user. The affiliate is logged in, navigating through all of these pages with a revised primary navigation menu. You now want to change the front page to reflect that page created for when the user logs in, creating a separate browsing experience for the user. It’s like creating a shell in which the user resides, keeping them contained within the affiliate experience.

Customize Your Front Page in Drupal

There’s a module that makes switching out your front page super easy, and its conveniently titled Front Page. Once this module is installed and enabled, you can configure the front page for affiliate and set your select mode to redirect, then add the node path in the path field. Be sure to click Front Page Override to allow this redirect.  Save your changes and check out the front page for a logged in affiliate. All of the content available on that new node page that an affiliate sees after logging in will now be their front page view as well.

The drupal creating content front page

The possibilities beyond this are endless with Drupal, and the aforementioned modules, and your ability to swap and add based on rules and roles knows no bounds. Keep in mind that you aren’t limited to the modules presented here, and should always explore the Drupal community for insight and other ways to customize a specific user’s experience.