2

In D7, using profile2 module, you would load the entity_metadata_wrapper from user object then access profile fields as such: $userWrapper->profile_XXX->field_XXX

Given that Profile2 is replaced by Profile module in Drupal 8, what is the Drupal 8's equivalent of accessing profile module's fields from a user object?

3
  • can you debug the user object using kint() and show the output Apr 2, 2018 at 12:39
  • Try accessing the value like this $userWrapper->profile_XXX->entity->field_XXX->value Apr 2, 2018 at 13:23
  • Dumping values for User object ($user->toArray() as well as kint()) won't show anything related to profiles... Using the D7 form (profile_XXX) doesn't work either To be more specific, I have a profile type called Teacher. User uid 300 has a Teacher profile entity with some values filled in. $user = \Drupal::entityTypeManager()->getStorage('user')->load(300); ksm($user->profile_teacher); // NULL. ksm($user->toArray()); // Nothing related to teacher.
    – bohemier
    Apr 2, 2018 at 19:49

4 Answers 4

2

Profile module add a base field to the user entity for every profile type. so it's quite easy to get all the referenced profiles then access any field you want :

$profiles = $user->get($profile_type_id . '_profiles)->referencedEntities();

if you want the first profile.

$profile = $user->get($profile_type_id . '_profiles)->entity;
1
  • Yes! This is it, thank you Alaa Aloui :) Now we can finally chain profile data! Example combining PHP 8 null-safe: $name = "My name is {$user->main_profiles->entity?->field_first_name->value} {$user->main_profiles->entity?->field_last_name->value}";
    – bohemier
    May 23, 2023 at 9:16
5

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 :-)

1
  • 1
    Yes, this is what I also ended up doing since there doesn't seem to be a way to directly query the user profile fields from the user object...
    – bohemier
    Sep 22, 2018 at 3:47
2

Profile storage offers a helper method to load a profile by user.

$profile = \Drupal::entityTypeManager()
  ->getStorage('profile')
  ->loadByUser($account, 'profile_type');
0

You can access the value of profile field values as below

$user = \Drupal\user\Entity\User::load(2);
$test1_value = $user->field_profile->entity->field_test1->value;

Before that make sure there is value in the user data enter image description here

4
  • Thanks @razeem-ahmad. Are you using the profile module? I do not have that autocomplete entity reference widget (profile field as shown in your screenshot) on my user pages. The Profile module provides tabs for each profile on the user account page... So there is no actual field_teacher in my user account, although I can manage the Teacher field in Manage Display...
    – bohemier
    Apr 3, 2018 at 11:46
  • you need to add a field_teacher for profile entity reference in user manage fields. You can do this here ~/admin/config/people/accounts/fields/add-field like as in above case. Then you need to add data in it. Apr 3, 2018 at 11:52
  • Thanks, now I see where it comes from. Somehow I feel this would bypass Profile functionality since a profile is automatically added by the user when they click on a profile tab in their account. That new profile entity would then need to be linked in those entity reference fields and that would yield 2 profile fields in manage display... It's an interesting solution but a bit too involved just to be able to access profile entities from a user object. It would be better to just use QueryInterface to query profile entities...
    – bohemier
    Apr 3, 2018 at 16:07
  • Yeah, this is not the right answer. @bohemier, you're right it seems querying the profile entities is the right (only) way to go. Sep 20, 2018 at 13:33

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.