10

Example: I want to get all tag names that are inside field_tags of node 5.

I can get the target_id with $node->field_tags->getValue();

enter image description here

But the problem is, I don't know what entity the target_id value belongs to. Could be a node id, term id, user id. Without that info, I am not able to use the target_id to get the name of the tag.

How can I get all the names of an entity reference field?

2 Answers 2

21

The easiest way to get what you want is something like this:

$names = [];
foreach ($node->field_tags as $item) {
  if ($item->entity) {
    $names[$item->entity->id()] = $item->entity->label();
  }
}

->entity is a so called computed property, it doesn't show up in getValues(). All reference fields have it.

And yes, forget about print_r() on entity or other complex objects. They contain objects that reference each other, and print_r() can't handle that. If you have an entity, always use print_r($entity->toArray()), then you get the field values only. You could install devel module or use a debugger, but that will give you the internal structure of an entity, which is not really want you want to see.

5
  • 2
    $node->toArray(); sure looks a lot more shorter & cleaner than using \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'article');
    – No Sssweat
    Aug 28, 2016 at 8:59
  • Where do you find documentation on this?
    – AlxVallejo
    Feb 19, 2018 at 17:09
  • You can find some information here for example: drupal.org/docs/8/api/entity-api
    – Berdir
    Feb 19, 2018 at 20:33
  • @Berdir: print_r($entity->toArray()) It solves this issue. I can able to get all fields details. Thank you so much.
    – Kalidasan
    Jan 18, 2019 at 6:20
  • @Berdir can you be more specific about the documentation? I've been looking through that page but don't see any specific examples related to term reference fields.
    – neuquen
    Feb 9, 2019 at 17:27
8

An entity reference field, by definition, can only target one entity type so you can know what your target type is by calling up getSetting() on the field definition.

// Print the targeted entity type field.
$field = \Drupal\field\Entity\FieldStorageConfig::loadByName('node','field_tags');
echo $field->getSetting('target_type');

Or better yet, use EntityReferenceFieldItemList::referencedEntities() to pull up the node's referenced entities and the associated data you need.

// Return an array of Entity objects referenced in the field.
$node->field_tags->referencedEntities();
5
  • print_r($node->field_tag->referencedEntities()); seems to be a resource hog, I had upgrade my AWS instance, just to be able to do it, since I kept getting WSOD.
    – No Sssweat
    Aug 28, 2016 at 4:41
  • Let me know if this should be a separate question. How can I access the name value, when in the array it says values:protected which means it can only be accessed inside the class.
    – No Sssweat
    Aug 28, 2016 at 6:28
  • I feel bad about un-accepting your answer, but @Berdir answer gave me no choice but to. I never figured out how to get the value using your Or better yet method. The first method is good, but it will require if statements to check the target type and then get the value. Berdir's answer is more efficient as there is no need for checks.
    – No Sssweat
    Aug 28, 2016 at 8:44
  • 1
    Don't sweat it (...pun intended). The first version of your question wasn't too clear on your objective. You should mark the answer that best fits.
    – Shawn Conn
    Aug 30, 2016 at 7:13
  • I really wish this answer wasn't buried within this SO post. I wanted to get the target_type value from an entity_reference field and searched high-and-low for it... until you answered my question here.
    – neuquen
    May 24, 2019 at 16:35

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.