开发者

How to make Drupal redirect to pages after user registration

I have a Drupal website and I want to show different welcome pages, depending on what my users enter as profile fields. I can't use 开发者_如何学编程the global $user variable, because users are not automatically logged in (They have to very their email address before they can log in).

Where can I add code to set the redirect? I've tried with $form['#redirect'] and $form_state['redirect'] in the form validator, but that didn't work.


You can use logintobogan for inspiration:

#implementation of hook_user
mymodule_user($op) { 
  if ($op == 'login') {
    $_REQUEST['destination'] = '/user/will/be/redirected/here'
  }
}

The important part is to make sure, that by the time the final drupal_goto() is called in user.module, you have set your $_REQUEST['destination'].

A few things to note:

  1. Logintoboggan has a lot of code to deal with all sorts of edge-cases, such as redirecting out/to https. You can ignore these, if your case is simple.
  2. Your module must be called after user.module and probably after other modules implementing hook_user, for they might change this global too. Very ugly, but the way this works in Drupal.
  3. Do not -ever- issue drupal_goto() in any hook. Especially not hook_user, or hook_form_alter. drupal_goto will prohibit other hooks from being called; breaking functionality at the least, but often corrupting your database.
  4. Do not issue drupal_goto() in form_alter callbacks such as "_submit", this might break many other modules and might even corrupt your database.


Similar to Berke's answer, but it seems like you just want this to be a one time thing. For that, you can check for the $account->access property to check their last login. If it is 0, then they are logging in for the first time.

This should work fine for email or no email validation.

<?php
/**
 * Implements hook_user().
 */
function mymodule_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'login':
      // execute this if they have never accessed the site before
      if ($account->access == 0) {
        // run conditional logic based on profile fields
        // to set destination here
        $_REQUEST['destination'] = 'path/to/welcome-page';
      }
      break;
  }
}
?>


I suggest you use the Login Destionation module or you can use the Rules module redirect action which is maybe to robust for your purpose.

Just in case you don't want to write your own custom module :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜