Score:0

How do I get the entity ID from a view row and pass it along to the item list template?

au flag

I have a view which returns rows of entities. Each entity has several multi-value fields.

The structure is kinda like this:

  • Field 1:

    • Item A
    • Item B
    • Item C
    • ...
    • Item H
  • Field 2:

    • Item A
    • Item B
    • Item C

Field 2 holds values that are related to Field 1 Item B. When displaying Field 1 in the view results I'd like to be able to display them like:

Field 1: Item A, Item B (Field 2 Item A, Field 2 Item B), Item C, Item D

Because its a multi value field Views is using the item_list template. My plan was that I would adds some conditionals to this template so that if Field 1 Item B was to be shown, I would use Twig Tweak to add the values from Field 2 inline to it. The only problem is that to do that I need the row's entity ID. For the life of me, I can't figure out how to pass this information into the item_list template. This template seems completely removed from the view and doesn't have a way to reference it.

I've been trying various functions in my theme file to get the result row NID and pass it along as a variable into the list_item template, but have had nothing but failure. I've figured out how to get a custom variable into the item_list template preprocess_item_list, but I can't figure out how to get the row NID into this function.

Any help would be appreciated.

sonfd avatar
in flag
It's probably easiest to create a custom Field Formatter for Field 1 rather than try to do this stuff in preprocesses / twig.
Score:0
cn flag

Add the node id as a hidden field to the view, then inside the templates {% for item in items %} you should be able to reference it as item.content['#node'].0.nid|raw but you may need to kint item.content to find out the actual value.

fallenturtle avatar
au flag
It actually is a field in the view, but I can't access it from the list item template.
fallenturtle avatar
au flag
I ended up doing it in a more hackie way. I found I could get what I want using the non-Views view display by replacing the output in the template of the Views field with a Twig Tweak field function essentially bypassing the Views rendering of the content.
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.