2
votes

Drupal 7 with relevant Modules: Views & Entity Reference

I have a view that simply shows the "Teaser" display type of two different Content Type records, we'll say they are Article and Page.

My Article CT has some simple fields that are shown.

My Page CT has a few fields, but also an Entity Reference field (unlimited entries). The Entity Reference field points to other Article nodes.

My View then, as expected, shows each record's Teaser display and of course when a Page record references another Article record, that Article's teaser is shown as part of the Page teaser.

This is great. The problem is, I do not want to show duplicate Article teasers. That means, if a Page references an Article, I don't want to show that same Article teaser as an individual listing.

Think of it like this, I have these Articles:

  1. Article 1
  2. Article 2
  3. Article 3

And I have these Pages:

  1. Page 4
  2. Page 5
  3. Page 6

Page 4 points -> at Article 1 and Article 2

Page 6 points -> at Article 2

TL;DR

Right now the view would show something like:

  • Article 1
  • Article 2
  • Page 4
    • Article 1
    • Article 2
  • Page 5
  • Page 6
    • Article 2
  • Article 3

What I want is this:

  • Page 4
    • Article 1
    • Article 2
  • Page 5
  • Page 6
    • Article 2
  • Article 3

EDIT: Here's a quick diagram of what I'm trying to do. This is using "Content" instead of "Fields" for the View by the way, since there's unequal fields in the View of the two CTs.

Diagram

3
did you show indentation in the output for explaining it, or do you want the output to be grouped like that. I want to know cause usually views gives a flat listD34dman
@D34dman To explain it. It's not an issue of styling or the like, and I already have the view outputting what I need, it's just that it's outputting duplicates as explained. The problem is the "duplicates" only exist as nodes that are referenced by other nodes. So one way to think of it (though I don't think this is possible), is I need the View to do: List all nodes of a 2 different types, A and B. If a node, type B, references a node, type A, do not show it when listing all Type As.Christopher Cooper

3 Answers

3
votes

The problem is that the records actually are distinct from a database perspective as there are multiple records generated by different entity references.

The Drupal 8 core issue for this is Views relationships with multi-valued entity reference fields invalidate Distinct query option. There should be usable patches there.

For Drupal 7, use the Views Distinct module as it probably won't be fixed in Views.

2
votes

I ended up knocking this out by making use of the Corresponding Entity Reference module (CER). This creates a 2-way relationship that is sync'd between the parent<->child. Using that, I could simply filter all children which have a parent from the View.

2
votes

From the view page under Advanced section. Click on Other -> Query settings: Settings. Then check Distinct checkbox.