2

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

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
Christopher Cooper
  • 1,910
  • 4
  • 20
  • 25
  • 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 list – D34dman Nov 29 '12 at 08:57
  • @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 Nov 30 '12 at 16:48

3 Answers3

3

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.

colan
  • 2,818
  • 2
  • 20
  • 17
2

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • 1
    Sadly not :( The problem is that the "sub" query is what is non-unique, not the main query where Distinct would take effect. The view selects all of CT #1 and CT #2 (all Distinct, no problem) - the issue comes in when the view shows the entity references from CT #2 that point at CT #1 records. – Christopher Cooper Dec 03 '12 at 16:03
  • @ChristopherCooper You are correct. See my answer with actual working solutions. – colan Jan 16 '20 at 17:29
2

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.

Christopher Cooper
  • 1,910
  • 4
  • 20
  • 25
  • 1
    wow @Christopher Cooper, nice you found the solution. I was not aware about CER's existence. Thanks for sharing the answer. – D34dman Dec 09 '12 at 13:15
  • 1
    This is an unnecessary workaround. My answer doesn't require something like this. – colan Jan 16 '20 at 17:46