I have Drupal 7 installed on my WAMP localhost. I need to add a widget to the account page to allow users to chose 1 of 2 roles (recruiter or applicant). Having chosen one role the user cannot switch back to the other again. For particular reasons I do not consider possibility to chose role DURING registration.

I have read and searched a lot on this subject recently but have not yet found the solution. I understand it can be done with the help of hook_form_alter() in a module? Then I need to update the users_roles table

Could you please guide me a bit through the code or tell me where I can find some examples.

Thank you.

Meanwhile I moved on - I have created the module with hook_form_alter(). The 2 radiobattons show up on non admin user's page (see attached screenshot).

The code is missing saving to the database and probably validation is also required - I dont know where to go yet. Looking forward to your comments/corrections of the below code which is far from being professional.

 * Implements hook_form_alter().
global $user;
if($user->uid !== 1) {  
function testpwd_form_alter(&$form, &$form_state, $form_id) {
#   krumo($form_id);

    switch ($form_id) {
        case 'user_profile_form' :

// Adding radiobuttons
            $form['account']['roles']['#type'] = 'radios'; 
            $form['account']['roles']['#title'] = 'Role';
            $form['account']['roles']['#weight'] = -20;
            $form['account']['roles']['#access'] = true; 
            $form['account']['roles']['#default_value'] = '';
            $form['account']['#access'] = true; /*without this Roles do not show up on autheticated 
            user's profile page. I think after setting it to TRUE access for the nested elements has also 
            changed to TRUE. I need to check what changed and set access to False for some nested elements.  */

            $form['account']['current_pass']['#access'] = false; /*see comment 1 line above. 
            Not needed on this page as for changing passwords users are directed to Master site 
            (2 sites joined with Bakery sso module).*/      

            $form['account']['roles']['#options'][115362423] = 'applicant';
            $form['account']['roles']['#options'][100482362] = 'recruiter';

//To hide Roles options appeared. Where hide() does not work I used unset() 
            unset($form['account']['roles']['#options'][30037204]); // admin
            hide($form['account']['roles'][2]); //auth users
            unset($form['account']['roles']['#options'][29617163]); //content admin


//nice print of arrays
            echo '<pre>', print_r($form, true), '</pre>';


enter image description here


The drupal module Auto Assign Role allows you to do this (users can select during registration, not afterwards).

The module name is a little misleading because although one of its features is allowing for automatically assigning roles to users, another of its features is to allow users to select a role for themselves.

  • Thanks for reply. I tested this module - it assigns roles DURING registration whereas I need this AFTER registration, that is to add this possibility to account page. ps. I have edited my Quastion acc-ly.
    – Alex777
    Apr 16 '15 at 6:44
  • No problems. I added a note to my answer that states registration only. I know it doesn't help you in this case but I'll leave my answer here in case it's useful to someone else.
    – rooby
    Apr 16 '15 at 6:48

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.