Created
November 22, 2017 15:41
-
-
Save Trench94/e031160fc2ab0a4bf49168e36d7d6ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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