Score:0

Read entity reference node field

in flag

I have a referenced entity named - field_cp_level2_ref_level3. This field belongs to level3 content type. when I dump the $entity, I see the field as below:

entity:node:level3::field_cp_level2_ref_level3 => Drupal\Core\Field\EntityReferenceFieldItemList (9) Depth Limit

how to read the value of this field? I tried to read the value as below:

$var =  $entity->$node->field_cp_level2_ref_level3->value;

dpm($var) returns null. how to read this field?

Score:1
ph flag

You can use $entity->field_x->referencedEntities(); which will return an array of referenced entity objects

in flag
doing so, throws the below error - "Error: Call to a member function referencedEntities() on null "
Lambic avatar
ph flag
That suggests there's nothing in your field. Maybe wrap it in an is null check.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.