2

I have user login block on custom page, I try:

if ($form_id == 'user_login_block') {

    $form['#redirect'] = '/';//or <front>

}

But I am redirecting always to the same page.

4

The user login block form is a special case. You will see that in the #action property, it defaults to /node?destination=node. The "destination" is the important piece, as it will override many of the normal avenues you would take to redirect the form including a $form_state['redirect'] in a submit handler. The code below is an example of how you can redirect the user_login_block form by modifying the #action property.

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ('user_login_block' == $form_id) {
    $path = 'path/i/am/redirecting/to';
    $form['#action'] = url($_GET['q'], array('query' => array('destination' => $path)));
  }
}

Also, take a look at hook_form_FORM_ID_alter(), which is the preferred method for altering the form for performance reasons.

4
  • What version does this code apply to, just Drupal 6, or Drupal 7 aswell? – iStryker Jul 23 '11 at 14:43
  • I tested this out with Drupal 7, but looking at the D6 code the concepts should apply there as well. – Chris Pliakas Jul 23 '11 at 14:47
  • Just to comment on my own code, a better option for building the #action property would be to set it to url($_GET['q'], array('query' => array('destination' => $path))) – Chris Pliakas Jul 23 '11 at 14:57
  • Modified code in answer based on my comment in an effort to post code that adheres to best practices. Sorry about the switch. – Chris Pliakas Jul 23 '11 at 15:03

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.