1

I have installed the Profile module. I am trying to programmatically load profiles. The code works, but I am not able to access any field value.

class IdCardController extends ControllerBase {

  /**
   * Init id card download.
   */
  public function initDownload(User $user) {
    
    $user_profile = $this->entityTypeManager()->getStorage('profile')->loadByProperties([
      'uid' => $user->id(),
      'type' => 'personal_information',
    ]);
    kint($user_profile);
  }
}

Using kint(), I can see all methods are protected.

screenshot

0

1 Answer 1

3

You could examine the Profile Entity class here profile / src / Entity / ProfileInterface.php, showing you a list of public methods including its getter methods, eg.:

$profile->getCreatedTime();

Additionally and similar as with Entities in general, you can also call data via either $entity->get('field_name')->getValue() or $entity->field_name->getValue().

See more on that here: Get information from an entity/ Entity methods

So if you have a Profile Entity with a field machine name address, then you could get its data like:

$profile = \Drupal\profile\Entity\Profile::load(123);
$profile->get('address')->getValue();
$profile->address->getValue();
1
  • 1
    OR $entity->field_name->value;
    – No Sssweat
    Nov 23, 2020 at 1:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.