0

I'm attempting to programmatically create test users in Drupal 8 via hook_update_n(). I have an array of user attributes I'm using to create users.

I've got three fields added to the User entity that add term references to existing vocabularies that have been populated with terms. Two of these fields store a single term reference, while the third can store multiple term references.

I've retrieved the term object using this code.

    $term = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->loadByProperties(['name' => $user['bu'], 'vid' => 
    'tx_business_unit']);

I'm trying to add a term reference to my user field, called "user_bu".

I've attempted to use the solution given on Programmatically update an entity reference field, but it doesn't work for me.

Everything else on my user record is being populated without issue.

0

1 Answer 1

0

For those of you who may be looking to answer the same question, here's how I got it to work in my case (I'm working on a Drupal 8.3 site):

$users = [
  'test_local_contributor' => [
    'password' => '1testme1',
    'langcode' => $language,
    'preferred_langcode' => $language,
    'email' => '[email protected]',
    'roles' => [
      'employee',
      'local_contributor',
      'testuser',
    ],
    'location' => [
      'Kentucky',
      'Paradise',
    ],
    'sbu' => 'energy',
    'bu' => 'natural_gas',
  ],
  ...(there were other test users, omitted for brevity)
];

foreach ($users as $key => $user) {
  $newUser = User::create();
  $newUser->setUsername($key);
  $newUser->setPassword($user['password']);
  $newUser->enforceIsNew();
  $newUser->setEmail($user['email']);
  $newUser->set('langcode', $user['langcode']);
  $newUser->set('preferred_langcode', $user['langcode']);
  foreach ($user['roles'] as $role) {
    $newUser->addRole($role);
  }
  $newUser->set('status', 1);

  if (isset($user['bu'])) {
    $term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $user['bu'], 'vid' => 'tx_business_unit']);
    $keys = array_keys($term);
    $newUser->set('user_bu', $keys);
  }

  if (isset($user['sbu'])) {
    $term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $user['sbu'], 'vid' => 'tx_sbu']);
    $keys = array_keys($term);
    $newUser->set('user_sbu', $keys);
  }

  $loc_keys = array();
  foreach ($user['location'] as $location) {
    $newkeys = array();
    $term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $location, 'vid' => 'tx_user_location']);
    $keys = array_keys($term);
    $newkeys = array_merge($loc_keys, $keys);
    $loc_keys = $newkeys;
  }
  $newUser->set('user_location', $loc_keys);

  $result = $newUser->save();
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.