Skip to content

Instantly share code, notes, and snippets.

@japicoder
Created September 6, 2017 14:35
Show Gist options
  • Save japicoder/6888124db2a5611c881cd8f763f5b454 to your computer and use it in GitHub Desktop.
Save japicoder/6888124db2a5611c881cd8f763f5b454 to your computer and use it in GitHub Desktop.
Drupal 8 - How to retrive the profile of an user
/**
* The following example allows to obtain the profile of a given user.
*
* @param $user \Drupal\Core\Session\AccountInterface
* User or UserSession object.
* @param $profile_type string
* Bundle type of the profile.
static function getUserProfile(AccountInterface $user, $profile_type) {
$profile = $this->profile_storage->loadByUser($user, $profile_type);
$entity_type_manager = \Drupal::entityTypeManager();
$profile_entity_type = $entity_type_manager->getDefinition('profile');
$profile_storage = ProfileStorage::createInstance(\Drupal::getContainer(), $profile_entity_type);
$profile = $profile_storage->loadByUser($user, $profile_type);
return $profile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment