0

I want to let users to select their role. There are 4 available roles (created by me as an admin) and the selection should be done after first login, so not on the registration form.

Can anyone help me to resolve this problem?

Note: the question let the user choose role upon registration is not exactly the same as my question here: that one contains "... with their chosen role ...", which is clearly something they entered on the registration form.

0

I am not sure, but you can try somthing lke this: create form with Form API, on submission change value of Role array in user object. Example:

function MENUCALLBACKNAME($form, &$form_state, $user) {
 $form = array();
 $form['role'] = array(
  '#title' => t('select role'),
  '#type' => 'select',
  '#options' => //Array of your roles like array(id_of_role => t('Name of Role'), ...)
 );

 $form['uid'] = array(
  '#type' => 'value',
  '#value' => $user->uid,
 );

 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Choose'),
 );

 return $form;
}

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

 $account = user_load($uid);

 $account->roles = $role;
 user_save($account);
 $form_state['redirect'] = 'SOME PATH';
}
  • I am using Profile 2 module, can You suggest me another solution ( module solution is preferable) – Vahan Sargsyan Apr 3 '13 at 14:08
  • for me it is more comfortable to write code then installing contrib modules and configure them and so on :) I do not know how Profile2 module works but role which is assined to user is located in user object. Proile2 module does not matter here. Just create simple module, create hook_menu, alter registration form submit and after registration redirect user to that page which you created with hook_menu. it's done:)... I do not know there is any contrib module which can do this functionality. – xurshid29 Apr 3 '13 at 14:19

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.