4
votes

I would like to load user profile module data by user id:

Code:

$profile_type = 'my_profile';
$user_id = 1;
$current_user = User::load($user_id);
$active_profile = $this->entityTypeManager()->getStorage('profile')->loadByUser($current_user, $profile_type);

print_r($active_profile);

But, It will return following error.

Error:

The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Argument 1 passed to Drupal\profile\ProfileStorage::loadByUser() must implement interface Drupal\Core\Session\AccountInterface, null given, called in C:\xampp\htdocs\catchow\htdocs\modules\custom\catchow_registration_contactlab\src\Controller\CatchowRegistrationContactlabController.php on line 122 and defined in Drupal\profile\ProfileStorage->loadByUser() (line 16 of modules\contrib\profile\src\ProfileStorage.php).
Drupal\profile\ProfileStorage->loadByUser(NULL, 'authenticated_user') (Line: 122)
2

2 Answers

6
votes

This is late, but other people will come here (just like I did).

I had to go into the profile module source code to find this, but I guess it's obvious when you think about it:

$list = \Drupal::entityTypeManager()
  ->getStorage('profile')
  ->loadByProperties([
    'uid' => $account->id(),
    'type' => 'profile_type',
  ]);

You'll probably want to wrap it up in a service - and if you don't, you should take a long hard look at yourself as to why not :-)

0
votes

Did you try to use user_load function instead of User::Load :

$current_user = user_load($user_id);

https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_load/8.2.x

And when you are dumping $current_user do you have some data ?