3

I have a Drupal 8.5.3 installation. I'm struggling to fix this, I have read many threads but none is exactly what I need, or at least I'm not being able to solve it: I have 2 content types: A, and B. They both have an entity reference field that references to another content type, C. I'm trying to create a block view to embed into any node of type A a list of B nodes that reference to the same C node that the current A page.

In other words:

Content type A: Cities

Content type B: Brands

Content type C: Countries

If node Rome (A) has a field that references to Italy (C), I'd like to display on it all the brands (B) that have also a field that references to Italy (C).

I have tried using relationships, contextual filters, providing default values from URL, etc. But I'm unable to make it work. I've tried selecting my entity reference field as a relationship, and then adding a contextual filter of Content ID that uses that relationship. I have also tried some other combinations but it's not working. I would appreciate some help!

Thank you very much!

2

It can be tricky to orient yourself around this, but it is a fairly common request to list sibling nodes according to whatever criteria (usually Taxonomy and Entity references).

The solution involving Entity references is to use two Relationships, one pointing to the common node (C), then another to point to the nodes that reference it (A) and then a Contextual filter that feeds the Views context on what node you're currently on (B).

  1. Create Views listing your content A
  2. Add a Relationship Content referenced from field_ref_C
  3. Add another Relationship Content using field_ref_C, and set it to use the Relationship from the previous step field_ref_C: Content
  4. Add a Contextual filter ID (from Content Category) and set it to use the second Relationship (field_ref_C the one from the previous step) with these options:
    When the filter value is NOT available:
    Provide default value
    Content ID from URL
    When the filter value IS available or a default is provided
    Specify validation criteria (optional)
    Content
    [your appropriate types - B]
  5. Add another Contextual filter ID but this time Do not use a Relationship:
    When the filter value is NOT available:
    Provide default value
    Content ID from URL
    More: Exclude

    This will exclude the node you're currently viewing from the list.

    If you want to check the output in Views Preview at this point enter the same node ID both times, for example to see what the block would output on node 14 enter 14/14.

    The first argument is taking the current node of type B and navigating through the connections, to the country C and back to content A you want to show, the second argument is excluding the current node from the list.

  6. Place your block in a region shown only on content B, don't forget to set the option Node from URL on block settings.

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.