I'm struggling to figure out how to programmatically assign a taxonomy term to a user in Drupal 7.
I have a term reference field [field_event_location] on the user registration screen. What I want to occur is to have an Administrator (role) create a new user and assign the meeting manager (role) to a set term.
Once the meeting manager(role) creates a new customer(role) through the user registration form, the term reference field will be automatically set to the term associated with the meeting manager user that was previously set by the Administrator.
I haven't found a contrib module that does this, so I was starting down the path of creating my own module using HOOK_user_presave
/**
* Implements hook_user_presave
*/
function mycustommodule_user_presave(&$edit, $account, $category) {
global $user;
if(isset($account ->is_new) && !empty($account -> is_new)) {
$roles = user_roles(TRUE);
if(in_array('meeting manager', $user->roles)) {
//set user they are creating to the role of 'customer'
$rid = array_search('customer', $roles);
$role = user_role_load_by_name('customer');
$edit['roles'][$rid] = 1;
//assign the customer to the same location as the venue admin
$edit['field_event_location'][LANGUAGE_NONE][0]['target_id'] = field_get_items('user', $user, 'field_event_location', LANGUAGE_NONE);
}
}
}