0

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.

1
  • 2
    It's probably easiest to create a custom Field Formatter for Field 1 rather than try to do this stuff in preprocesses / twig.
    – sonfd
    Oct 8 at 2:39
0

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.

2
  • It actually is a field in the view, but I can't access it from the list item template. Oct 8 at 16:44
  • 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. Oct 8 at 16:48

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.