Profile picture for user admin
Daniel Sipos
02 Sep 2012

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.

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.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Alex 25 Jan 2013 04:26

Reverse Concept

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?

Daniel Sipos 25 Feb 2013 11:53

In reply to by Alex (not verified)

Hey there, sorry for the late

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.

Edwin 19 Sep 2013 23:55

In reply to by admin

Photo Album preview listing

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?

Gletzer 24 Feb 2013 23:48

Amazing article!!, but still not working me?

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

Daniel Sipos 25 Feb 2013 11:28

In reply to by Gletzer (not verified)

Hello there!

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.

Gletzer 25 Feb 2013 13:30

I got it :) but not working

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

Daniel Sipos 25 Feb 2013 13:19

Unfortunately I cannot

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.

Gletzer 26 Feb 2013 01:45

In reply to by admin

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

Gletzer 26 Feb 2013 01:14

In reply to by admin

Found the culprit !! me ;)

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

jagjeet 12 Jun 2013 14:05

In reply to by Gletzer (not verified)

it is good u solved this

it is good u solved this problem.

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

really appreciable

Daniel Sipos 26 Feb 2013 10:53

Hehe, easy to make mistake. :

Hehe, easy to make mistake. :)

Glad it works now!

Stefan 06 Mar 2013 14:59

Great tutorial - short and sharp

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.

Chandeep 19 Mar 2013 17:17

Making it work the other way around

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.

Daniel Sipos 19 Mar 2013 17:00

In reply to by Chandeep (not verified)

It should be fine. You need

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?

Adri 03 Apr 2013 23:56

Not working for me

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!

Eric 08 Jun 2013 09:51

Not Working for Me

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.

Eric 09 Jun 2013 02:16

A slightly different approach that worked for me...

After much trial and error, the approach I describe in this blog post successfully displayed related content of the same content type and excluded the node being viewed: http://ericgilbertsen.com/2013/06/create-related-content-blocks-drupal-7-views-entity-reference/

Ivor 08 Jul 2013 15:57

Directional relation

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?

virgilsam 15 Oct 2013 16:30

Random Selection

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.

Martin 15 Jan 2014 17:15

Up until this part,

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

Akram Wahid 28 Jan 2014 17:01

Need Urgent Help

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

drupalsh 17 Jul 2014 20:17

Great post! How to work with unlimited referencing entities?

Hi, this is a great , great post and I've got it somewhat working except my original entity reference allows for unlimited references and with this solution I only get the first referencing entity -- not all of them. Any advice or modifications I can make?

nordicmaterial 22 Oct 2014 19:53

Works great, also in the other direction!

Thank you for sharing this, works great!

To achieve this in the other direction:

  • create a view (a Block)
  • add Relationship of the type: Entity Reference: Referenced entity.
  • require this relationship
  • add contextual filter (nid) as described above
  • When the filter value is NOT available: same as described above
  • When the filter value IS available: Specify the validation criteria box > Validator: select Content of type Related Content this time!
  • Place the block in a visible area in the Related Content pages.
Travis Geis 15 Feb 2016 22:11

You are a God!

This seems like such a common requirement for a CMS, but it took me so long to stumble upon your article! Thanks so much!

Mark Anderson 23 May 2016 03:19

Taxonomy Terms with Depth

How can we get this View to work with Taxonomy terms that have depth? For example, if the main node is tagged with a parent term, I would like it to display all referenced nodes that are tagged with child terms nested under the parent term.

Add new comment