0

How can I add the role to registration form that users can choose their role?

I tested the Profile2 module and the Auto Assign Role module but they don't do what I want.

I want to add a CCK field in the admin/configure/account/fields.

Is there any module to add a CCK field to registration form for choosing the role?

  • 1
    Did you test the module? drupal.org/project/user_selectable_roles – ya.teck Jul 26 '13 at 15:58
  • I believe you can combine drupal.org/project/profile2_regpath with profile 2. These would allow you setup 2 or more registration paths. You can then tell your users to Register as student or Register as a teacher. – seyi Jul 26 '13 at 16:15
  • This module has become redundant and will NOT be ported to Drupal 7. I recommend using Auto Assign Role, which does the same thing and more. – mahdi alikhasi Jul 26 '13 at 16:20
  • No I don't to have different address for registration. at now my problem is adding role selection to the cck of the registration form – mahdi alikhasi Jul 26 '13 at 16:21
1

If any module doesn't fit yoour requirement then you can use the below code You can create any custom module or put this code in template.php of your theme. function testing_custom_form_alter(&$form, $form_state, $form_id){

if($form_id=="user_register_form"){
    $result = db_select('role', 'r')
                ->fields('r',array('rid','name'))
                ->condition('name',array('administrator','anonymous user'),'NOT IN')
                ->execute()
                ->fetchAll();
    $role   =   array();
    foreach($result as $userRole){

        $role[$userRole->rid] = $userRole->name;
    }

$form['role']['#type'] = "select";
$form['role']['#options'] =$role;
$form['role']['#required'] = 1;
$form['role']['#size']='auto';
    $form['#submit'][] = 'custom_assign_role';

} 

}


function custom_assign_role(&$form, $form_state){
   $uid=  $form_state['values']['uid'];

    user_multiple_role_edit(array($uid), 'add_role', $form_state['values']['role']);

}

Hope it will help!
1

Role selection actually already exists on the user registration form, it's just not accessible for unauthenticated users.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  // Display the roles selection.
  $form['account']['roles']['#access'] = TRUE;

  // Get available roles.
  $roles = array_map('check_plain', user_roles(TRUE));

  // Unset system roles.
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
  unset($roles[DRUPAL_ANONYMOUS_RID]);

  // Add options without system roles.
  $form['account']['roles']['#options'] = $roles;
}

Where the user_account_form is built: https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_account_form/7

Where the user_account_form is included in the user_register_form: https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_register_form/7

0

You can add field called as field_role over here admin/config/people/accounts/fields and assign various roles to it.You can use select list.

Based on role selected while registering you can assign that role to the user by using rules module.

  • Rules module you can compare text comparison for the selected field_role by user while registering.
  • Based on that you can assign user the role what you want to assign for each text.

Example:

     Seller as Seller(Roles)

     Editor as Editor(Roles) etc  

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.