Pregunta

I would like to redirect a user that logged in over the user login block. What I have is a module that contains the following code:

Appendix, 3.9.2011, 15:30h: Changed code according to the advice of kiamlaluno.

Appendix, 3.9.2011, 17:08h: Small Fix: Changed node/admin to admin.

Appendix, 3.9.2011, 17:24h: removed [] -> code is working like this now, but do not forget to change the module priority in DB.

function _MYMODULE_user_login_submit($form, &$form_state) {
  global $user;
  if ($user->uid == 1) {
    $form_state['redirect'] = 'admin';
  } elseif ($user->uid) {
    $form_state['redirect'] = 'node/add/image';
    return;
  }
}

/**
 * Modifies the outfit and behaviour of the user login block.
 */
function MYMODULE_form_user_login_block_alter(&$form, $form_state) {
    unset($form['#action']);
    // removes the forgot password and register links
    $form['links'] = array();
    // Redirects the user to the image upload page after login
    // This cannot be done by a rule, the rule based redirect is only
    // working for the login page not the user login block.
    $form['#submit'] = array('_MYMODULE_user_login_submit');
} 

It doesn't redirect users; it seems like _MYMODULE_user_login_submit() is simply ignored.

What I know already:

  • I cannot use Rules/Triggers, because I do not login over the login page but the user login block
  • It is always said: "use logintoboggan" on posts, but there I only have redirection options for "on registration" and "on confirmation", but I need "on authentication" or "after login".
  • Anyway, I do not want to use more modules, I prefer a few lines of PHP.
¿Fue útil?

Solución

Your code doesn't work because user_block_login() sets the "#action" property for the form; in that case, redirecting the form after submission doesn't work.

  $form = array(
    '#action' => url($_GET['q'], array('query' => drupal_get_destination())), 
    '#id' => 'user-login-form', 
    '#validate' => user_login_default_validators(), 
    '#submit' => array('user_login_submit'),
  );

To make it work, you should first unset $form[#action], and then executing the code you already execute in your hook_form_alter() implementation.

As side notes, I will add:

  • If you want to be sure your code effectively redirect the user where you want, be sure your module is executed for last; if any other module that implements hook_form_alter() adds a form submission handler to redirect the user to a different page, and that module is executed after yours, then your module would not have any effect. To make sure your module is executed after the others, you should use code similar to the following during the installation of the module, or in an update hook. (Replace "MYMODULE" with the short name of the module.)

    db_query("UPDATE {system} SET weight = 100 WHERE name = 'MYMODULE');
    
  • Instead of using MYMODULE_form_alter(), you can use `MYMODULE_form_user_login_block_alter(), which would not require to check the form ID.

  • You should append new form submission handlers, instead of replacing the existing ones. This means you should use $form['#submit'][] = 'user_login_submit_redirected';.
  • Functions implemented in a module should be prefixed with the short name of the module, which means "MYMODULE_" or "_MYMODULE_" (the latter is for private functions). Not using such prefix could create a compatibility issue with other module, such as the User module, as the function you are using has a name starting with "user_."

Otros consejos

can u try this please

function user_login_submit_redirected($form, &$form_state) {
  global $user;
  if ($user->uid == 0) { 
    $form_state['redirect'] = 'node/admin';
    drupal_goto('node/admin') ;
  } elseif ($user->uid) { 
    $form_state['redirect'] = 'node/add/image';
    drupal_goto('node/add/image') ;
    return;
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top