2

I have to render a field of a node that is inside a content type in Drupal 8.

I have a content type called summary that has a field, called field_content that is an Entity reference field for unlimited items of a specific content type (article)

I'm able to get the current node and handle the field_content, but I cant access each of referenced items inside of this type.

Also I have tried to debug the field_content with kint but the browser crashes (I suppose that it happens becasue the data dump is very big) and I can't access the inner properties (I have to get a field called field_desc_text inside each article).

I need the node or the nid to load it of each of this inner items.

This is what i've tried:

       $currentNode = \Drupal::routeMatch()->getParameter('node');
    foreach ($currentNode->get('field_content') as $nodeRef)
    {
      kint($nodeRef); //*This debugs the entire referenced node, but there is no sight of my desired fields
      die;
     // kint($nodeRef[0]); *Cannot use object of type Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem as array
     // kint($nodeRef->get('title')); *Uncaught PHP Exception InvalidArgumentException: "Property title is unknown."

     die;

    }

And this is the output of the kint($currentNode):

https://mega.nz/#!Fogx0LpC!CYvc6YSRs1ZcgOAc4RCfDo5iVqjBLYckvuFEUgqRGx8

Thanks in advance!

3

I believe you need $currentNode->get('field_content')->referencedEntities() -this should return each of the referenced entity objects.

  • 1
    Thanks both for saving my day. @No Sssweat: That fixes the kint error about dumping data. And even now I can low the level to render the kint debug faster or increase it to dive into kint variables. BrynJ: That was what I was looking for. Due the kint was not loading well all the data, i coulden't see that method. Thanks both again! :) – cadev Feb 3 '17 at 13:40

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.