4

I want to do page redirect when user access /user page. When user anonymous user access the /user it should redirect to user/login page and when logged in user access the /user page it should redirect to user/%/edit page.

I have tried the following the code

function my_user_menu_alter(&$items){
   $items['user']['page callback'] = 'my_user_page_redirect';
} 
function my_user_page_redirect() {
  global $user;
  if (user_is_logged_in()) {
  drupal_goto('user/'.$user->uid.'/edit');
}
 else {
   drupal_goto('user/login');
 }

}

For logged in user it redirects to user/%/edit page. For anonymous user it displays webpage redirect errorenter image description here

How can I do this. Thank You

3
  • From user to user/login already happens for anonymous users. Drupal does that without any third-party modules.
    – apaderno
    Sep 25, 2015 at 5:05
  • I have custom login form.
    – user50991
    Sep 25, 2015 at 5:06
  • It's not the login form that does that.
    – apaderno
    Sep 25, 2015 at 5:08

1 Answer 1

1

You can do it without hook_menu_alter, achieve it in hook_init, try something like (don't forget changes in hook_init need clear the cache to getting applied)

function yourmodule_init(){
   if(current_path()=="user"){
   global $user;
     if (user_is_logged_in()) {
      drupal_goto('user/'.$user->uid.'/edit');
    }
   else {
      drupal_goto('user/login');
   }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.