Folks,

I'm going crazy trying to redirect to a specific URL after login, I've found a ton of posts on it but it doesn't seem to work for me and I have no idea why.

function mymodule_form_alter(&$form, $form_state, $form_id) { 

    if (($form_id == 'user_login') || ($form_id == 'user_login_block')) {
			unset($form['#action']);
			$form['#submit'][] = 'mymodule_submit_function';
    }
}

function mymodule_submit_function($form, &$form_state) {
	$form_state['redirect'] = 'mypage';
}

From everything I've read, the above should work. I have verified that the form_alter is being hit and if I look at $form at that point, I see the "mymodule_submit_function" in the array, but login still just redirects to /user

I've also tried in the form alter
$form['#action'] = url('', array('query' => array('destination' => 'mypage')));

Which didn't work, I also tried just using $form['#redirect'] which also didn't work.

What am I missing? hook_form_alter is being hit like it should but no matter if I try to do a redirect there or at a submit handler and try to do it there, nothing works.

Any ideas?

TIA

Comments

vijaythummar’s picture

Thanks,
Vijay Thummar

cfaust’s picture

Why though? I just want a single special case redirect, I don't want to install a module on all my sites just to do 1 redirect in a special circumstance.

I don't understand why what I'm doing is not working.

nevets’s picture

If nothing else you can look at the login destination module for how they do it.

cfaust’s picture

I did, they just do it via hook_user, which does work but I don't see how I can special case it, I only want to redirect to a custom url in a specific case, not redirect every login

nevets’s picture

Does your code work the same login destination? Can you get your module to work so it always redirects like login destination? Then the question becomes when do you want to redirect?

cfaust’s picture

I can get the same stuff that login destination does to work, it looks like that is the only approach I have.

cfaust’s picture

So after trying everything under the sun, the only thing that works for me:

function mymodule_form_alter(&$form, $form_state, $form_id) {

    if (($form_id == 'user_login') || ($form_id == 'user_login_block')) {
        $_SESSION['custom_redirect'] = 'custom_path';
    }
}

then

function mymodule__user($op, &$edit, &$account, $category = NULL) {
   if ($op == 'login') {
		if ((isset($_SESSION['custom_redirect'])) && (!empty($_SESSION['custom_redirect']))) {			
			$_REQUEST['destination'] = $_SESSION['custom_redirect'];
			unset($_SESSION['custom_redirect']);
		}
}

Not a good solution IMO but doing the submit handler or validation handler or any of the other suggestions simply do not work with the login form.