Login Destination snippets

By Erik, Wed, 11/03/2010 - 15:03
Typically, upon login, users are directed to the page from which they logged in. If that is "/user" then you are brought back to the user profile page. If you would like people to be sent to specific pages after logging in, there are a couple of modules that can help. LoginToboggan (http://drupal.org/project/logintoboggan) is one of the most common login tools, and as the project page shows, it does many different things from allowing users to login with their email address, giving you a small horizontal login block. It also includes the option to allow users to be sent to a specific page the first time they login (this is when they validate their email address by clicking on the link that was in the email or by entering the system generated password for the first time). However, if you'd like your users to be redirected to the same page every time they log in, you need Login Destination. The Login Destination (LD) module (http://drupal.org/project/login_destination) lets you configure where users end up when they log in. LD provides two ways to send users to a specific location. The first is by entering a static string which can be a system path or alias (i.e., node/234 or content/my-story-title). One nice feature is that if you have a path alias for your node, it will send the user to the alias even if you use the node value (this is more important later). In our scenario, we had Organic Groups (OG) set up with each group having a "homepage" (which is actually the group node). Most users were only members of a single group (state/district-based groups). But some were members of multiple groups and also in an "Staff" role. So we wanted everyone in the Staff role to be redirected to one page, while everyone else should be redirected to the homepage of the one group of which they were a member. LD allows us to also use PHP to return a string (or array) for the destination. This allows us to do some basic logic to return different values based on various conditions. So first, if we're members of the Staff role, we should redirect to PageA: roles))) { // $user->roles is an array of which roles the user is assigned since it is an associative array, // array_values will look at the values of each element in the array return 'PageA'; } ?> Then, we need to add the code to send group members to their group homepage. og_groups)) { // the $user->og_groups property holds the groups of which a user is a member. The current() function // will look at the first value in the array by default. return 'node/' . $group['nid']; // the $group object has a property of "nid" which will return the node id for the group // returning 'node/124', for example will send you to the group homepage // and the magic is that if you have a path alias for that node, (groups/Maryland, for example), // the alias will show up as the actual URL for the user } ?> If you don't meet either condition, normal Drupal rules would apply and you will most likely be sent to the page from which you were logging in or you can add a statement to return the value 'front' which LD interprets to mean send them to the homepage.

Comments1

broyhillg (not verified)

13 years 4 months ago

In your example of a login redirect for the role Staff, I assume you paste this in the PHP Snippets box for Redirection Conditions, substituting the name of my role where you used 'Staff' and substituting my destination node where you used 'pageA'.

So what do you put in the Destination URL Settings box, since you've already defined the redirect to 'pageA' in the PHP code?

Thanks!