I scoured the web looking for help building a ‘Related Content’ Block for nodes of the same content type in Drupal 7 using Views and Entity Reference, as opposed to Taxonomy. In my case, I was building a basic CRM tool and wanted to display related interactions with a contact on the interaction detail page (i.e. node). For example, if I was looking at notes from a phone call with a customer in April, the block would show that I emailed the same customer in March and met him at a conference in February.

Other uses could include displaying songs by the same artist or articles by the same author. Taxonomy isn’t always a good solution because it is only a term and not a full piece of content.

The best article I found was on webomelette.com, however it didn’t work for everyone, including myself. After much trial and error, I found the solution. While I’m not a developer – PHP scares me – this is pretty advanced so I will assume your content types and View is already created.

Notes About My View

In my case, I wanted to display a table with information about related interactions, such as the date (linking to the node), contact (linking to the referenced entity), type (phone, email, meeting, etc.), and notes.  I also had some fun with ‘rewrite output’ to create a “Add notes” link if the Notes field is empty.  (Under No results behavior, enter <a href="/node/[nid]/edit">Add notes</a>.)

Important: This block appears on a node page of the same content type.  You wouldn’t use it for a block of songs on an artist page.

Full View Display

Full View Display

Step 1: Add Relationships

Two relationships must be added in this order:

  1. Entity Referenced: Referenced Entity
  2. Entity Referencing: Referencing Entity

The Referencing Entity must reference the first relationship, the Referenced Entity. I have no idea why, but it creates a circular relationship of some kind.

Entity Reference: Referenced Entity settings

Entity Reference: Referenced Entity settings

Entity Referencing: Referencing Entity settings

Entity Referencing: Referencing Entity settings

Step 2: Add Contextual Filter Using Second Relationship

The next steps tell the View to only display content that share the same entity, such as all interactions with the same contact in my case.

  1. Add a Contextual Filter using Node ID (Nid) that uses the Referencing Entity relationship
  2. Select Provide default value and choose Content ID from URL
Contextual Filter using Referencing Entity

Contextual Filter using Referencing Entity

Step 3: Add Contextual Filter Using No Relationship

If you stop after step 2, you’ll see a duplicate node in your block. So if I’m looking at notes from an interaction, that same interaction would appear in my block instead of only related nodes.

To exclude the node you’re on, follow these steps:

  1. Add another Contextual Filter using Node ID (Nid), but don’t select a relationship
  2. Select Provide default value and choose Content ID from URL
  3. Under the ‘More’ menu, select Exclude
Contextual Filter with no relationship

Contextual Filter with no relationship

Contextual Filter with 'Exclude' option selected

Contextual Filter with ‘Exclude’ option selected

Step 4: Choose ‘Distinct’ Option in Query Settings

I’m not entirely sure if –or why – this is needed, but the description says “This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work.” Sounds good to me.

Query settings 'Distinct' option selected

Query settings ‘Distinct’ option selected

Step 5: Save

The final step, of course, is to save the View and display the block on your node pages. In my case, the block only displays on pages with interaction/* in the URL.

10 Comments

  1. webmast

    Thank you very much for your help, Eric. I was having trouble getting this to work at the beginning, until I realized that it must be songs to have an Entity Reference field that points to the artist, not the other way around. Thanks again.

  2. Danny

    Hey there. Thanks for mentioning my article on Web Omelette.

    Just wanted to point out that for those people who followed my method there, the solution worked. Some tend to leave out small details breaking their results. 

    Cheers!

    Danny

  3. Selwyn Polit

    Nice article. Got me exactly where I needed to go. Works fine even if the content type to display is different from the related content one. I was displaying a resort content type and wanted to show related articles.

    thanks
    Selwyn

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>