2

I have a login form block, when a user fail to login to the site I want to redirect him to the user/login page with the usual error message

6

You can do this by form_alter

function MODULE_NAME_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_login_block':
      $form['#validate'][] = 'my_custom_login_val';
      break;
    default:
      break;
  }
}


function my_custom_login_val($form, &$form_state) {
  $form = form_get_error($form['name']) . form_get_error($form['pass']);
  if($form != '') {
    $_GET['destination'] = 'user/login';
    drupal_goto('user/login');    
  }
}

If you need like after the correct login redirect to a page use Login Destination

1

Here a slightly different answer using hook_form_alter

function MODULE_form_alter(&$form, &$form_state, $form_id)
{
    if ($form_id == "user_login_block")
    {
        $form["#validate"][] = "MODULE_validate_login";
    }
}

function MODULE_validate_login($form, &$form_state)
{
    // on successful login, the uid is set in form_state.
    if (empty($form_state["uid"]))
    {
        $_GET["destination"] = "user/login";
        drupal_goto("user/login"); 
    }
}
-1

Got this to work in drupal 7, looks like you need to define both

$_GET['destination'] = 'user';

AND

$GLOBALS['destination'] = 'user';

$_GET['destination'] = 'user';
$GLOBALS['destination'] = 'user';
drupal_goto('user');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.