Related Content block with Views and Entity Reference - Drupal 7

This tutorial will teach you how to create in Drupal 7 a Related Content block that shows content related to the node that is being rendered on the page. To do this you'll need the Entity Reference and the Views modules. The relation between the nodes is made by a reference between the two, but can also be by sharing the same taxonomy terms.

Related Articles:
10 cool Drupal modules that integrate with Views
Create a View with Views inside in Drupal 7
Cool module: Views block area
How to display random results with Views in Drupal 7

So, you do this in two steps:

Step 1:

First, you logically need at least 2 Content Types: one for the main node and another as reference nodes. Once you know what you want to reference to what, you have to install the Entity Reference module.

For the purpose of this tutorial, I will refer to the main Content Type as Article CT and the referenced content as Related Content CT.
Once you installed the Entity Reference module, add a new field to your Article CT. Go to Structure – Content Types – Article (or whatever your Content Type is) – Manage Fields – Add New Field – Entity Reference. For the widget, I used autocomplete (but you can choose which one to use). In the field setting, you can select the target Content Type, in this example the Related Content CT.

To test if this is working so far, create a test node of your Related Content CT. Then, create a node of your Article CT. In the edit form of the Article CT you should find your newly added Entity Reference field. Find the Related Content node you just created and then you can save the node. View the Article CT node and you should see a link to the Related Content node you just added to your Article node. In case nothing appears, check to see if the newly added field is set to display in the Full View mode. Go to Structure – Content Types – Article (or whatever your Content Type is) – Manage Display and you can set it there in case it is not.

Once you checked and it appears properly on the node page, go back and hide the newly added field from the display on all view modes of the Article CT. You will display this content with Views in a block using Contextual Filters.

Step 2:

If you hadn’t already, install the latest version of Views and create a new View with a block display that shows only Related Content CT. On the right side, open the Advanced options and add a new Relationship of the type: Entity Reference: Referencing entity (you’ll find the description underneath as being A bridge to the Content entity that is referencing Content via field_related_content – or whatever you named the Related Content field in the Article CT). Click Apply and on the next screen check the box: Require this relationship” and add an Identifier to what you prefer.

Next you need to add the Contextual Filter in order to display results in this block that relate only to your Article CT.

Add a Contextual Filter of the type: Content:Nid and click Apply. On the next screen, under Relationship, select the Identifier of the Relationship you had just added in the Relationship section. Below, under When the filter value is NOT available, select Provide default value of the type Content ID from URL.

Lower, under When the filter value IS available or a default is provided, check the Specify the validation criteria box and as Validator select Content of type Article (or whatever your main content type is). Under Filter value format choose Node ID and under Action to take if filter value does not validate choose Hide View. Then you can Apply, save your block and test.

Add your block to a region and go back to your Article node and when viewing that node, your newly created block should display the title of the Related Content node you referenced in the Article node earlier. You can of course work a lot with that new View block and customise it as you please.

Hope this helps, good luck, and drop a comment if you didn’t understand something.

Comments

Okay I have two Node Content Types. The first one is called Image, the Second Album. Image nodes have an entity reference field to reference albums.

I want to make a block that displays other images in the album. The block would go on an image page and display in the sidebar other images in the reference album. I can't seem to make this work. Help?

Hey there, sorry for the late reply but what you want to do falls exactly withing the scope of this post :) If you follow the instructions you should be able to get it working quite easily.

First of all: Nice article!
But I don't know how to get it working on a collection of albums. This is what I try to achieve:
- In facebook you can find "ALBUMS" in the photo section. On that page you can find a collection of photo albums.
- Each album shows the first image of that particular album. Only one.
- When you click an the preview, the album opens with other images visible.

My approach was making some content-types
- Album
- Photo
A Photo page has a reference to an Album page.

Next two views:
- AlbumView
a listing of published Photo page thumbnail images
- AlbumOverview
a list of the published albums
including ONE thumbnail of the first image in that Album.

The problem is, when I create the AlbumOverview view, I get a listing of ALL the images in that album.
Question: How can I limit that to just the first image in that album?

Danny, I want to thank you for an amazing article. Almost nothing on the internet on this exact subject, at least a real life saver for me.. except it is not working for me. Any chance you could assist me no this? If I leave out 'Require this relationship' with Relationships and leave out Contextual filters I get a list of all referenced nodes used by the reference field.

Gletzer

Hello there!

You are not supposed to leave out those things. They make it so that the View results only shows content referenced through the entity reference field of the node that is displayed on the page. So if node 5 has a reference only to node 25, when you view node 5 you should see in that block only node 25. This is done via the contextual filters that look at the node ID as taken from the URL and by using that relationship. It is all explained in he article :)

Did you try however doing it in another way? A bit more flexible and dynamic if you are not bound to entity reference.

Hi,

Cheers :) Your article explains everything in detail very well, so no problem following your method.. and I followed it by the letter. But still not working. When I left out those things I mentioned, something appeared (without any limits) - I mentioned this so you might possibly have an idea of what the problem might be. I am going to check this out on a clean install and see if I cannot get this working.
In connection with doing it another way, do you have any recommendations or suggestions besides the taxonomy method? What I want to do is replicate the function of the books module. I find the books module rather limited and using ´entity reference´ module with the 'inline entity form' module one is able to create a very efficient interface for editors to work from and by using reference the possibilities are very broad. Still I would prefer using the 'relation' module, as that module is simply amazing, but with fairly limited interfaces - and so not as user friendly.

Gletzer

Unfortunately I cannot replicate your problem. Actually..when checking to see if it works, are you looking in the Views preview or you also put the resulting block on the page and tested it there?

Just FYI, the Views preview will not show any results with these contextual filters..unless you also pass the write context in that box. So best is to put the block on the relevant page to see if it works.

Hi again,

With clean install everything works, so I simply create a new content type on current site that I was having trouble with and everything is working, which is great :)

Gletzer

After having a better look, I think the reason it was not working is simple enough.. I selected 'Entity Reference: Referenced Entity
A bridge to the Content entity that is referenced via my_reference_field' instead of 'A bridge to the Content entity that is referencing Content via my_reference_field' in Relationships.

Gletzer

it is good u solved this problem.

i am also having same problem. please give a detailed walkthough of this.

really appreciable

I was doing the same mistake.
Thank you Gletzer and this great article author

Views is great, but not always very intuitive as far as I think. Thanks to your explanation I found the way to create a content aware block.

Thanks, this is great an easy to follow. It works as described but I require the opposite to work if you can please assist.

I 2 content types, one is a parent landing page, and I have lots of child content to be displayed.
On each child the entity reference refers to the Parent.
I implemented your method and it works to show the parent in a block on each child page, but I had hoped it would work the other way around instead - parent page shows related children. Is this possible with the entity reference field as it currently is? It would be ideal form a usability and training perspective.

I have thought of implemented the taxonomy method but only want parents to show related children, not the other way around too which is what it seems to imply.

hey there!

Isn't the solution just placing the views block to show on the parent pages and then the block will show the content that is referencing the parent?

First of all, thanks for this clear and concise explanation. I've been trying to reproduce this but i cannot get it to work. I got "Property" as main content and "City" as referenced. Before setting contextual filters and relationships I'm able to list all cities i have in a node, but after setting contextual filter "Nid" and relationships "referencing entity" view is displaying nothing. Could you help? Is there any other approach appart from taxonomy?
Thank you in advance!

Thank you for the article! I'm having the same problem as many of these readers and hoping for troubleshooting advice.

My content types are Contact and Interactions (like a phone call or email with each contact). I want to show related Interactions on an Interaction node. My block is using the Referencing Entity (not Referenced), Required is checked, my Nid uses this Relationship and is configured exactly how it is described above. My block is empty, even though I have two published nodes that share the same Contact. I've tested with and without 'Distinct' under query settings as well.

Thanks for the article Danny, it's clearly details how to set this up. I am having a similar problem Chandeep but I'm not sure if it is my understanding or a limitation (my money is on the former) on the relation. I have two content types one is used to catalogue astronomical objects and the other an image with details of how it was taken. The relationships are N-N e.g. Messier 31 can appear in multiple images and likewise multiple astronomical objects can appear in an image.

My plan was to add the relationship to the astronomical objects when the image was uploaded. Following your tutorial what I have found is the block will only display fields from the content type in first endpoint in the relationship and not the second.

Am I missing something?

Hey there! Great article and I have mine working great...except for one thing. I was wondering if there is a way to have a random node of the same content type show up as the related content. For instance, I am building a bookstore and I want a "Users Also Viewed" (which I have), but, I was wondering how to make random other books show up and possibly be different every time. In other words, have random nodes of the same content type show up so I don't have to type in the related content manually.

Up until this part, everything has worked. Entity Reference: Referencing entity doesn't show my Referenced entity as an option. Any idea what might cause this ?

Great guide, Thanks

First of all: Nice article!
But I don't know how to get it working on a collection of video albums. This is what I try to achieve:
I have a page with collection of Video Albums .
- Each album shows the cover image of that particular album. Only one.
- When you click on the title or cover image, the album opens with other videos inside that album,

My approach was making two content-types
- Video Gallery , (title,cover image)
- Video Gallery Item (title,video,refernce_to_video_gallery)

A video Gallery item has a reference to a video Gallery.

Next two views:
- Video gallery View
a listing of published video galleries with cover images and linked to a page where it will list all the video gallery items, under it

The problem is, when I create the video gallery view, I get a listing of ALL the video galleries, with title, and cover image,
as i am a newbie , i dont know how to link each video gallery title and cover image to a page where it will pull all video gallery items, how to limit result, actually I need an idea to complete this,

Some Body Please give me an idea how can I achieve this

Add new comment