By Anonymous (not verified) on
When the user logs in, I want a default page to be shown, not the current page the user is on when they login. And even more, I want different pages to show for different user types.
For example, I'm building a training events database and there are trainers, volunteers, and participants (people registered for training events). I want a different page for each of these "roles".
I imagine I'll have to do a bunch of PHP work to get this to happen. Has it been done before? Can someone show me the way to get started?
Thanks!
Comments
user.module
You have to make a custom login block/page.
Check out the way that the forwarding is done in the default login block: in
user.module, the login form is created by theuser_blockfunction ($delta=0). The line of interest to us is:$output = form($output, 'post', url('user/login', drupal_get_destination()));The third argument to the
formfunction is the "action" parameter of the HTML form tag - the URL to which the form will be submitted. If you pay attention, you see that it is compiled from'user/login'and the return value ofdrupal_get_destination().The
drupal_get_destination()function returns a string containting an elementdestination=query_stringThis argument is then used in the user module after the user is authenticated, in adrupal_goto()statement.Now to answer your question, all you should have to do is create a new login form somewhere, with the line
$output = form($output, 'post', url('user/login', drupal_get_destination()));changed to something like
$output = form($output, 'post', url('user/login?destination=path_to_page_you_want_to_go_to_after_login')));If you want to do something more complex like redirecting the user based on roles, then the best way is probably to create a php script at the location that you redirect them to, that checks what roles the user belongs to and redirects them accordingly with another
drupal_gotoHope this helps.. sorry if its a little techincal, but what you are trying to do is not trivial.. you should be able to find documentation on this site about each of the individual steps of this process, but if you have problems feel free to ask here again
not trivial, but doable
Thanks very much for that explanation. I've been getting into the innards of Drupal enough lately that I understand what you're saying and believe I can actually get that to work the way I want. I just needed to know where to start--and that's exactly it.
David Cornelius
Possible to do with a module
While you could do this by modifying user.module you can also do it with a module and make it more flexable in the process.
I have a module (4.5 and 4.6) that allows the administrator to define a login destination per role plus a rule for the page to displaying after logging out.
separate module
Thanks for replying to me privately as well. I like your idea of a separate module better than modifying the core code. Makes updating easier. But if I had to, now I know where it is.
David Cornelius
You might want to...
take a look at Front Page module. You may also find Sliced Bread PHP Snippets interesting.
lots of examples
Thanks for pointing these out to me. There are many ways to cut this and I'm finding more and more examples of just how powerful and flexible Drupal is all the time.
I've used the PHP snippet thing in a psudo-static page on another site, but it displays the same thing from the database whether or not someone is logged in. It was a good learning step.
David Cornelius
wow..someone remembers!
Hi inspired by life...
Totally off topic, but, I had to post and say fair play to you for remembering the "original" title of those php snippet pages.
I can probably understand why it was changed, but, I sorta liked the "sliced bread" moniker. It was less clinical and more inviting than just "php snippets". (And so much easier to search for "sliced bread" snippets than "php" snippets!)
Judging by the volume of sliced bread snippets in there now, I reckon others find it a useful section as well..
cheers!
Dub
Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate
nice code samples
nice code samples