1

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);
      }
    }
 }
2
  • 1
    Please specify your Drupal version, 7 or 8? thx.
    – No Sssweat
    Sep 6, 2016 at 17:23
  • @NoSssweat Drupal 7
    – MRC
    Sep 6, 2016 at 17:38

2 Answers 2

1

Have you considered using Rules?

You could have it react on updating an After saving a new user account,

enter image description here

check for the role being set,

enter image description here

Click on Add Action, select Set Data Value

enter image description here

For DATA Data Selector put account:field-[name] In mine I named/called the term reference field test

For VALUE Data Selector put site:current-user:field-[name]

We use site current user, because the current logged in user is the Meeting Manager, so we use their term ref field value.

enter image description here

Result:

This is how it should look like for you. Except for you the authenticated user in condition should be customer and in Action the test should be your term ref field name.

enter image description here

4
  • I need to match the term of the current user with the same term as the meeting manager who is adding the user. How would I do this with rules?
    – MRC
    Sep 6, 2016 at 19:40
  • The question ask for how to programmatically assign a taxonomy term to a user in Drupal 7. This answer use rules which is a way to do it without programming. It works (until you want more complexe term selection logic), but it's not programmatic. Sep 6, 2016 at 19:56
  • @MRC Updated the answer, took me less than 15 seconds to create this rule.
    – No Sssweat
    Sep 7, 2016 at 1:56
  • @NoSssweat I actually like this answer a lot and should work for my use case, but I did want to figure out a programmatic approach.
    – MRC
    Sep 7, 2016 at 14:41
0

To programmatically do something when a user register, right before the user is actually saved to the database you can implement hook_user_presave().

function MODULE_user_presave(&$edit, $account, $category) {
  global $user;
  $manager_role = user_role_load_by_name('meeting manager');
  $customer_role = user_role_load_by_name('customer');
  if ($account->is_new && user_has_role($manager_role->rid, $user) && user_has_role($customer_role->rid, $account)) {
    $edit['field_FIELD_NAME'][LANGUAGE_NONE][0]['target_id'] = MODULE_get_manager_term($user)->tid;
  }  
}
6
  • Can you elaborate on this last line = MODULE_get_manager_term($user)->tid; @Pierre
    – MRC
    Sep 7, 2016 at 0:29
  • That would be a custom function to get the term associated with the meeting manager user that as previously set by the Administrator. It's actual implementation depends on how you associate a term with a meeting manager. Sep 7, 2016 at 4:01
  • If you want to actually copy the value(s) for the same field from the meeting manager, you can simply use $edit['field_FIELD_NAME'][LANGUAGE_NONE][0]['target_id'] = field_get_items('user', $user, 'field_FIELD_NAME', LANGUAGE_NONE); Sep 7, 2016 at 4:03
  • I have updated the question with my code and tried to implement your solution within the hook, but I receive an undefined index error. @Pierre
    – MRC
    Sep 7, 2016 at 14:28
  • Sorrym itr should have been $edit['field_FIELD_NAME'][LANGUAGE_NONE] = field_get_items('user', $user, 'field_FIELD_NAME', LANGUAGE_NONE);. But I don't think that would be the cause of a "undefined index" error. Which line trigger the error, and what's the index? Sep 7, 2016 at 18:42

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.