2

I have two user roles with profile fields.

Role 1
Name, Username, Password, Email, Address line 1, Address line 2, Website.

Role 2
Name, Username, Password, Email

I want to let anonymous user to chose user role on registration.
Upon user role selection, I want to show role-related fields interactively (with javascript).
I mean that when select Role 2, Address line 1, Address line 2 and Website fields will be disappeared, and when select Role 1, they will be appeared again.

I currently use 'user_role_field' module and 'autoassignrole' module.
Is there another module to achieve that purpose ?
I'm using Drupal 7.7.

Here is what I'm looking for. Click here to download and view screen draft.

Thank you for any help.
Rgds/Sithu

2 Answers 2

1

You could use Profile2 to get you started. It will allow you to assign profile fields to roles and have them available upon registration. You could then use jQuery to display the proper register form on selection.

7
  • So, I need to write custom module ??
    – Sithu
    Jan 10, 2012 at 6:07
  • Not sure. Can you give me more information on how you want this to work Jan 10, 2012 at 7:48
  • I updated my original post with a link to my screen draft to download.
    – Sithu
    Jan 10, 2012 at 10:20
  • As Drupal 7 core provides a way to add profile fields, do we need to use Profile 2 module which requires Entity API ?
    – Sithu
    Jan 11, 2012 at 2:57
  • It seems like you could add a custom module solely for adding some jquery to your registration form. Jan 11, 2012 at 3:10
1

I grouped the fields (Address line 1, Address line 2 and Website) by using field_group module. And hide the field group from jquery upon selecting role.

1) I created a js file ( mymodule.js )

Drupal.mymodule = {
    toggleProfile : function(obj){
        if($(obj).val() == 6){
            $('#user_user_form_group_profile').hide();
        }else{
            if(typeof $(obj).val() != 'undefined') $('#user_user_form_group_profile').show();
            else $('#user_user_form_group_profile').hide();
        }
    }
};
$(document).ready( function(){
    Drupal.mymodule.toggleProfile($('input[name=user_roles]').filter(':checked'));
} );

2) Included it from hook_form_alter and attached "onclick" attributes on the user role radio buttons.

if($form_id == 'user_register_form'){
    # User role
    $form['autoassignrole_user']['user_roles']['#attributes'] = array('onclick' => "Drupal.mymodule.toggleProfile(this)");
    if($user->uid == 1){ # if master administrator, hide it
        $elems[] = 'autoassignrole_user';
    }
}

One drawback is that js uses "rid" from the "users_roles" table, i.e., $(obj).val() == 6
If there is a better solution, it would be really appreciated.

Your Answer

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

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