By alexbea


2016-04-20 21:47:03 8 Comments

For context, I'm dealing with two content types: article and team_profile. In article nodes, we have an entity reference field pulling a team_profile node in to serve as the author information.

I would just render the entity with a new display mode, but our design has the photo set directly above the published date and name, next to one another: screenshot of the author information design

In D7 I would dpm($content) and drill down to the node information, double click, and pull out the rest of the reference path. I'm using kint and am able to spit out the entity reference contents to a certain level, {{ kint(content.field_blog_author[0]['#node']) }}. The next level I want to access is values, but trying the following gives me NULL:

  • content.field_blog_author[0]['#node’][‘values’]
  • content.field_blog_author[0]['#node’]=>values
  • content.field_blog_author[0]['#node’]->values

I'd ultimately like to render the title and field_team_image fields from the referenced node (seen here in the kint output). I haven't found information about this is the documentation or by searching. Would someone help me get unstuck?

1 comments

@Berdir 2016-04-20 23:17:31

#node there is actually the current node.

If you want to access fields and their values, start off with node, not content, which is a render array with the configured fields.

You can access the reference entity like this {{ node.field_blog_author.entity }}.

From there, you can continue again to the field of the referenced entity that you want to display. You could try calling the view method, with will view it with the default settings, you can also pass a view mode as a string to it or an array display settings (not sure about the exact syntax for that in twig).

Also, my standard reference, see http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet for a great overview of content entities, how they're structured and how to access values. Most of that also applies to twig.

And last, when trying to figure out what's in a content entity, use node.toArray(), that gives you an array representation that's very close to how you can access it as an object. While the internal structure is quite different.

@alexbea 2016-04-21 14:26:47

Thank you, Berdir. {{ kint(node.field_blog_author.entity) }} does output a nice array of the referenced node, but now I can't go deeper. {{ kint(node.field_blog_author.entity.title) }}, for example, breaks the page, loading no resources or anything past the header region. Same for nid or others. Just for giggles, trying to include the referenced entity in the node content with {{ node.field_blog_author.entity }} returned an error message seemingly saying that the node couldn't be shown in the template like that.

@alexbea 2016-04-21 14:31:42

It looks like these might be protected values. Does that sound right? Maybe I need to create a view mode for just the photo then use the "label" display mode for the name.

@Berdir 2016-04-21 15:08:14

Yes, they are protected. As I said, use toArray(), which also works on node.field_blog_author.entity.title is a field object, if you want to value, you need ...entity.title.value. Make sure to read the referenced cheat sheet, it explains a lot of those things.

@alexbea 2016-04-21 15:57:13

I appreciate the help. I don't really understand how to use the information in the cheat sheet, however. Adding .value did work in this case and answers my original question.

Related Questions

Sponsored Content

1 Answered Questions

Render entity reference field with link in node template

  • 2013-02-04 15:52:41
  • Smos
  • 3248 View
  • 1 Score
  • 1 Answer
  • Tags:   7 theming entities

1 Answered Questions

How do you override the add or edit node for a particular content type

  • 2018-10-01 20:43:06
  • Dupls
  • 478 View
  • 1 Score
  • 1 Answer
  • Tags:   8 theming hooks

2 Answered Questions

[SOLVED] How to get node url from Entity Reference field?

  • 2017-12-18 22:40:51
  • usernameabc
  • 1487 View
  • 0 Score
  • 2 Answer
  • Tags:   8 theming entities

0 Answered Questions

1 Answered Questions

1 Answered Questions

using several fields from another node using entity reference -- how?

  • 2017-09-16 22:37:16
  • idiotprogrammer
  • 209 View
  • -1 Score
  • 1 Answer
  • Tags:   8 nodes

0 Answered Questions

Default values when adding node using Entity Reference Prepopulate

  • 2017-06-20 03:35:55
  • uchuneno
  • 124 View
  • 0 Score
  • 0 Answer
  • Tags:   7 nodes

1 Answered Questions

[SOLVED] Render a field from an entity reference in twig template?

  • 2017-02-10 14:43:41
  • Brian
  • 1922 View
  • 1 Score
  • 1 Answer
  • Tags:   theming 8

1 Answered Questions

[SOLVED] How to find Image URI in Kint via .entity in the node tree

  • 2016-11-16 20:30:44
  • NicholasByDesign
  • 101 View
  • 1 Score
  • 1 Answer
  • Tags:   theming images uri

1 Answered Questions

[SOLVED] How to reference a view using entity reference field type?

  • 2012-10-05 09:48:02
  • Mark Christopherson
  • 4085 View
  • 2 Score
  • 1 Answer
  • Tags:   views nodes entities

Sponsored Content