Skip to content

Instantly share code, notes, and snippets.

@Trench94
Created November 22, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trench94/e031160fc2ab0a4bf49168e36d7d6ccc to your computer and use it in GitHub Desktop.
Save Trench94/e031160fc2ab0a4bf49168e36d7d6ccc to your computer and use it in GitHub Desktop.
/**
* Update the Drupal user with new team id
*/
public function updateUserTeam($username, $team_id) {
//Load user
$user = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties([
'name' => $username,
]);
$user = reset($user);
// If user object has loaded...
if(!empty($user)) {
$loadeduser = \Drupal\user\Entity\User::load($user->id());
$loadeduser->field_team->target_id = $team_id; // Change the team_id value
$loadeduser->save(); // Save the user
// Check if user has been updated
if($loadeduser->field_team->target_id == $team_id){
return true;
} else {
// If it hasn't return false
return false;
}
} else {
// If it hasn't return false
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment