Search code examples
http-redirectdrupaldrupal-6hook

drupal form with destination redirect


I am trying what should be a simple line of code but isn't working. With the following code:

drupal_goto("user/register?destination=/node/1");

I am sending my visitors to a registration page which once completed and they hit the submit button, I then want them re-directed to node/1.

The problem is my is implemented in hook_init and my sites goes into an endless loop.

Can anyone suggest how I might get the code to execute once or a more suitable drupal hook to implement my code?


Solution

  • If you want the redirection to occur specifically when the user submits the registration form, then you're gonna want to alter the user_register form by implementing hook_form_FORMID_alter and adding the '#redirect' attribute, like:

    /**
     * Implementation of hook_form_FORMID_alter().
     * @param $form
     * @param $form_state
     * @return void
     */
    function MYMODULE_form_user_register_alter(&$form, &$form_state) {
      $form['#redirect'][] = 'node/1';
    }
    

    That way, Drupal will register the user and then send him/her to node/1.

    Now, if you want this to happen after the user logs in (whether or not it is a "post-registration" login or a normal login), then you're probably gonna want to use drupal_goto() in an implementation of hook_user in the 'login' operation (that is, when $op == 'login')