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?


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

  • doing so, throws the below error - "Error: Call to a member function referencedEntities() on null "
    – chaydru
    Nov 1 at 19:13
  • That suggests there's nothing in your field. Maybe wrap it in an is null check.
    – Lambic
    Nov 1 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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