I want the user form to redirect to a different place when it's embedded on another page. I'm using drupal_get_form to fetch the form. Is there a way to extend forms and modify their behavior?

Comments

imrook’s picture

You can indeed change the form's behavior... but you don't need to if all you want to do is redirect the user after a login. The user module provides it's own hook (hook_user) that you can implement to catch a successful login and redirect the user. If you create a module called foo, your function would be foo_user($type, $form_values, $user). You only want to spring into action when $type='login' -- this is called after the user has been authenticated and a session started. A drupal_goto will probably be what you want to do next. Your code will be something like:

<?php
function foo_user($type, $form_values, $user) {
  if ($type == 'login') {
    drupal_goto('/mycoolpage');
  }
}
?>

Just for closure, you can indeed modify the form as well. The hook_form_alter hook allows you to edit any form as it is being built. You would check to see if the form was the user login form and if so add a '#validate' item to it. This allows you to register a custom validation function that is run during the form validation. Going this route you would have to do much more work by yourself, but since the user module already provides a great set of hooks you'll want to use those instead.

drupalninja99’s picture

another issue is that when I embed the user form on a different page if i'm logged in it makes that page redirect to the user home page which is bad. I hope the user hook or form alter can do something ab that.

drupalninja99’s picture

if ($user AND arg(0) == 'role_subscription') {
	drupal_set_message(t('You are logged in successfully, please continue with your subscription.'));
	drupal_goto('role_subscription');
}

Works well on the redirect part but it appears since I redirect it's not finishing update the user object which makes me still not logged in...whoops. Is there a drupal function for logging in a user?

drupalninja99’s picture

function role_subscription_user($op, &$edit, &$account, $category = NUL) {
	
	if ($op == 'login' AND arg(0) == 'role_subscription') {
		drupal_set_message(t('You are logged in successfully, please continue with your subscription.'));
		drupal_goto('role_subscription');
	}
}

This seems to work.

OK I also have the register form on the same page. Now the problem is, is that if I login as an admin it shows the admin register form. I guess I will hide it altogether. I kind of wish there was a way to force the normal register form though.

drupalninja99’s picture

I'm using a combination of form_alter and the user hook and that works fine for the user login. I also have an embedded register form and I'd like to know their user id. How do I get that user ID? Is there some form hook for user register?

imrook’s picture

I looked at the user.module code and didn't see any user_module_invoke calls (or any *_invoke calls for that matter) in the user_register_validate() function. My first thought is to use hook_form_alter to add an extra submit handler to the registration form. Documentation on adding a submit handler is here http://api.drupal.org/api/file/developer/topics/forms_api_reference.html... I haven't ever intercepted user registration data, so I can't say this is the best way -- just one that I'm sure will work. Perhaps someone else in the community who has more experience with this scenario can chime in.

drupalninja99’s picture

Anyone else have anything?