5

I'd like to let the front-end user to choose user role upon registration.

When the register form submit, I'd like it to redirect to the respective profile page concerning with their chosen role.

I use profile2 module and profile2 registration path module.

Is there any existing module for that ?

Thanks in advance!

closed as off-topic by kiamlaluno Dec 15 '16 at 7:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking to recommend or find a book, tool, module, theme, distribution, tutorial, or other off-site resource are off-topic for Drupal Answers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

4

If you love custom coding and if you already have a custom module, I would recommend to do this with a custom module rather than using Profile 2 and other related modules. I personally prefer to write custom modules because it will let you get exactly what you are looking for. But you still need to use Auto Assign Role module to let user choose a user role on registration.

My solution is to show/hide the respective profile fields upon user role selection on the registration form. It would not redirect to the respective profile page.
You can create profile fields at admin/config/people/accounts/fields and you can export them to the registration form, but they cannot be created by role. What you would need to do is to show/hide each field by role from hook_form_alter and use #ajax attribute.

Here is an code snippet for this purpose. I did not test the code. Just for example.

function yourmodule_form_alter(&$form, &$form_state, $form_id) {

    if($form_id == 'user_register_form' || $form_id == 'user_profile_form'){

        $form['autoassignrole_user']['user_roles']['#ajax'] = array(
          // #ajax has two required keys: callback and wrapper.
          // 'callback' is a function that will be called when this element changes.
          'callback' => 'ajax_form_alter',
          // 'wrapper' is the HTML id of the page element that will be replaced.
          'wrapper' => 'replace_field_div',
          // Don't show any throbber...
          'progress' => array('type' => 'none'),              
         );

        # This profile field will be shown or hidden when the respective "user_roles" is selected
        $form['field_first_name']['und'][0]['value']['#type']       = 'hidden';
        $form['field_first_name']['und'][0]['value']['#required']   = FALSE;
        $form['field_first_name']['und'][0]['value']['#type']       = 'hidden';
        $form['field_first_name']['und'][0]['value']['#prefix']     = '<div id="replace_field_div">';
        $form['field_first_name']['und'][0]['value']['#suffix']     = '</div>';

        // An AJAX request calls the form builder function for every change.
        // We can change how we build the form based on $form_state.        
        if(isset($form_state['values']['autoassignrole_user']['user_roles']) && $form_state['values']['autoassignrole_user']['user_roles'] == {rid_here}){
            # when the respective user roles is selected, show this field and set required.
            $form['field_first_name']['und'][0]['value']['#type']       = 'textfield';
            $form['field_first_name']['und'][0]['value']['#required']   = TRUE;   
        }else{ 
            # when the respective user roles, hide this field and remove required.
            $form['field_first_name']['und'][0]['value']['#type']       = 'hidden';
            $form['field_first_name']['und'][0]['value']['#required']   = FALSE;           
        }
    }
}
/**
 * Callback for user_roles
 *
 * On an ajax submit, the form builder function is called again, then the $form
 * and $form_state are passed to this callback function so it can select which
 * portion of the form to send on to the client.
 *
 * @return renderable array (the textfield element)
 */     
function ajax_form_alter($form, $form_state){
    return $form['field_first_name']; 
}

You may notice that you have to use ['und'][0]['value'] for the profile field to tweak its attributes. If you are not sure about this, you can print $form for debugging purpose:

dpm($form); // if Devel module is enabled
echo '<pre>'; print_r($form); echo '</pre>'; // if you don't have Devel module

If there is another better solution, please post it and it will be appreciated.

  • I adopt your solution as it suits my needs. Thanks for the help. However, I need to change a little. For one thing, change $form_state['values']['autoassignrole_user']['user_roles'] to $form_state['values']['user_roles']. And instead of using fixed {rid_here}, I use user_role_load_by_name(). – Pyone Kay Khine Nov 10 '12 at 19:46
2

checkout the Auto Assign Role.

The Auto Assign Role serves three primary purposes. The first is to provide an automatic assignment of roles when a new account is created. The second is to allow the end user the option of choosing their own role or roles when they create their account. The third is to provide paths that will trigger a specific role when an account is created. The administrator has full control over these functions and can have them working together or independently of each other.

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