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.
<?php
/*
* 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'][139881272]);
unset($form['account']['roles']['#options'][29617163]); //content admin
krumo($form);
//nice print of arrays
echo '<pre>', print_r($form, true), '</pre>';
break;
}
}
}