Drupal 7 - First Time Login Redirection
I was trying tocome up with a solution for first time login redirection and after that redirection to user profile edit page every time a user logged in.
For that I created a role, "First Time Login". Then created a rule using rules module. It will assign role "First Time Login" to a newly created user.
Then in the following code, in module "custom_login_redirections" using hook_user_login redirected user to the "Welcome Page" of it has role "First Time Login".
Then using "custom_remove_role_from_user" function, removed that role "First Time Login" from the user. So next time it should redirect to user edit page.
For that I created a role, "First Time Login". Then created a rule using rules module. It will assign role "First Time Login" to a newly created user.
Then in the following code, in module "custom_login_redirections" using hook_user_login redirected user to the "Welcome Page" of it has role "First Time Login".
// Comment
function custom_login_redirections_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
if(isset($account->roles[30])){
if(in_array($account->roles[30], $account->roles)) {
$options = array(
'absolute' => TRUE,
'query' => array('destination' => '#overlay=node/17973')
);
$redirect = url('user/register', $options);
$_GET['destination'] = $redirect;//drupal_encode_path('#overlay=node/17973');
custom_remove_role_from_user($account->uid, $account->roles[30]);
}
}
else {
$_GET['destination'] = 'user/'.$account->uid.'/edit';
}
}
}
Then using "custom_remove_role_from_user" function, removed that role "First Time Login" from the user. So next time it should redirect to user edit page.
// Comment
function custom_remove_role_from_user($user, $role_name) {
// For convenience, we'll allow user ids as well as full user objects.
if (is_numeric($user)) {
$user = user_load($user);
}
// Only remove the role if the user already has it.
$key = array_search($role_name, $user->roles);
if ($key == TRUE) {
// Get the rid from the roles table.
$roles = user_roles(TRUE);
$rid = array_search($role_name, $roles);
if ($rid != FALSE) {
// Make a copy of the roles array, without the deleted one.
$new_roles = array();
foreach($user->roles as $id => $name) {
if ($id != $rid) {
$new_roles[$id] = $name;
}
}
user_save($user, array('roles' => $new_roles));
}
}
}
Comments
Post a Comment