How to load the instance of referenced entity from the field in Drupal 8

You, DrupalDrupal entity api
Back

Loading the entity or entities from a field is a very useful method in numerous ways. So here are the possible ways to get access to it so you can load it up and access it's properties.

The easiest way:

$node->field_image->entity

The more detailed way.

$referencedEntity = $node->get($field)->first()->get('entity')->getTarget()->getValue();
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.