2

I have worked on a solution which will redirect from the user registration form to my own form. My own form can be reached by typing the website name testweb/my_module/form. Here is the code of my module using a form_alter. When I run the function it doesn't redirect to my own form. Could someone advice me why it is not working?

function redirect_custom_reg_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register') {
    $form_state['redirect'] = 'my_module/form';
  }       
}

4 Answers 4

1

It is $form['#redirect'], not $form_state['redirect'] in this case because user_register_submit() unsets $form_state['redirect'] (well, sets it to '') when it is confident that all is good with its part in the registration process...and hence allowing an alter to add in a #redirect.

1
  • Both of these are valid, see kiamlaluno's answer
    – Clive
    Commented Feb 18, 2012 at 19:00
1

Basing on the comment you left for this question, I get you want to show your own form; in this case, setting $form_state['redirect'], or $form['#redirect'] is not what you want to do, as the redirection take place after the form has been submitted, which means the form validation handlers, and the form submission handlers have been already executed.
From the user's point of view what would happen is:

  • The users see the default registration form used by Drupal.
  • The users enter the required information.
  • If they don't correctly fill the fields, the form is shown them again, together a list of error messages.
  • If they correctly fil the fields, the data is saved, and they are redirected to the page that has been set through $form_state['redirect'], or $form['#redirect'].

What you want is altering the registration form, and for that there are two cases:

  • You want to completely alter the existing registration form.
  • You want to simply add some fields to the existing registration form.

Completely altering the existing registration form

In this case, you implement [hook_menu_alter()][1] to alter the menu callback used when the registration form is needed.
The menu callback for the registration form is defined in [user_menu()][2] using the following array:

  $items['user/register'] = array(
    'title' => 'Create new account', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('user_register_form'), 
    'access callback' => 'user_register_access', 
    'type' => MENU_LOCAL_TASK,
  );

To alter it, you could use the following code in your module (replace "mymodule" with the short name if your module):

function mymodule_menu_alter(&$items) {
  if (isset($items['user/register'])) {
    $items['user/register']['page arguments'] = array('mymodule_user_register_form');
  }
}

mymodule_user_register_form() is the form builder function that return your registration form. In this case, you are completely overwriting the default registration form, which means Drupal doesn't have control over the registration process, and your code needs to handle the full process.

Adding some fields to the existing registration form

In this case, you implement [hook_form_FORM_ID_alter()][3] to add new fields to the registration form. This is what done from the System module (part of Drupal), which uses the following code to add the field that allows the users to set they timezone.

function system_form_user_register_form_alter(&$form, &$form_state) {
  if (variable_get('configurable_timezones', 1)) {
    if (variable_get('user_default_timezone', DRUPAL_USER_TIMEZONE_DEFAULT) == DRUPAL_USER_TIMEZONE_SELECT) {
      system_user_timezone($form, $form_state);
    }
    else {
      $form['account']['timezone'] = array(
        '#type' => 'hidden', 
        '#value' => variable_get('user_default_timezone', DRUPAL_USER_TIMEZONE_DEFAULT) ? '' : variable_get('date_default_timezone', ''),
      );
    }
    return $form;
  }
}

In Drupal 6, you would implement [hook_user()][4]; when $op (the first parameter) is equal to 'register', the registration form is going to be shown, and the hook implementation needs to return the form fields to inject in the form.
In Drupal 6, the function that has the same purpose of system_form_user_register_form_alter() is system_menu().

function system_user($type, $edit, &$user, $category = NULL) {
  if ($type == 'form' && $category == 'account') {
    $form['theme_select'] = system_theme_select_form(t('Selecting a different theme will change the look and feel of the site.'), isset($edit['theme']) ? $edit['theme'] : NULL, 2);

    if (variable_get('configurable_timezones', 1)) {
      $zones = _system_zonelist();
      $form['timezone'] = array(
        '#type' => 'fieldset', 
        '#title' => t('Locale settings'), 
        '#weight' => 6, 
        '#collapsible' => TRUE,
      );
      $form['timezone']['timezone'] = array(
        '#type' => 'select', 
        '#title' => t('Time zone'), 
        '#default_value' => strlen($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0), 
        '#options' => $zones, 
        '#description' => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.'),
      );
    }

    return $form;
  }
}

The function uses a parameter list that is different from the one reported for hook_user(), which is hook_user($op, &$edit, &$account, $category = NULL); what system_user() calls $type is called $op in hook_user() documentation.

1
  • I tried using your method but for some reason it does not seem to redirect I want my own form to be activate when the user clicks on the register new link . Then when the link is chosen I want it to redirect to my own form. I did try and add the weight but I am not sure I had the syntax correctly
    – alex
    Commented Feb 19, 2012 at 17:51
1

Alternatively, you can utilize the LoginToboggan module. Here is a quote about it (from the module's project page):

... offers several modifications of the Drupal login system in an external module by offering the following features and usability improvements:

  • Allow users to login using either their username OR their e-mail address.
  • Allow users to login immediately.
  • Provide a login form on Access Denied pages for non-logged-in (anonymous) users.
  • The module provides two login block options: One uses JavaScript to display the form within the block immediately upon clicking "log in". The other brings the user to a separate page, but returns the user to their original page upon login.
  • Customize the registration form with two e-mail fields to ensure accuracy.
  • Optionally redirect the user to a specific page when using the 'immediate login' feature.
  • Optionally redirect the user to a specific page upon validation of their e-mail address.
  • Optionally display a user message indicating a successful login.
  • Optionally combine both the login and registration form on one page. Optionally have unvalidated users purged from the system at a pre-defined interval (please read the CAVEATS section of INSTALL.txt for important information on configuring this feature!).
  • Integrates with Rules module to do various tasks when a user validates via email validation process.
0

you can use rules module to make actions after events, including redirect user after registration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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