0

I'm creating a simple site which has shows and episodes. I have created both content types and I added the entity reference field to the episode type. The episode would reference a show which it belongs to. I have a view which lists all the shows. However, when I click a show I want a list of all the episodes belonging to that show.

How would I achieve this?

1

You can create a view that lists episodes that references the show. This is done using the module Views.

Go to admin/structure/views/add and add a view, call it episodes and create a block display.

new view

Create a new view and a relationship, the label will be "Entity Reference: Referencing entity" and as description you will see "A bridge to the Content entity that is referencing Content via field_YOURFIELD".

Then add a contextual filter called "Content: Nid". In the settings for the filter look for "When the filter value is NOT available" and choose "Provide default value" and choose "Content ID from URL".

Your settings will look something like this (I removed the second column to make the image smaller).

View settings

Now go to admin/structure/block and place the block you created (if named the same as my example it will be called "View: episodes") and place it in the region you want it to show.

| improve this answer | |
  • This looks neat! I will report back later today if i get it working! – Ortix92 Jul 18 '14 at 10:25
  • Magic! It worked! – Ortix92 Jul 18 '14 at 18:09
  • I had to do a clean install of drupal and i retried this but for some reason the block is referencing the "show" content instead of the episodes belonging to it. In other words, the block is referencing the current page. This is my block settings page: i.imgur.com/vjNHWia.png – Ortix92 Aug 3 '14 at 20:55
  • The weird thing is that it does show the same amount of episodes which are referencing the show entity. It lists the show, but it lists it the amount of times that there are episodes. What is this black magic? – Ortix92 Aug 3 '14 at 21:01
  • @Ortix92 click on the field Content:Title. Under relationships tell it to use the Relationship in your view. – Pontus Nilsson Aug 4 '14 at 7:09
0

What you are after is basically covered by the Corresponding Entity References module. From the module description:

CER keeps reference fields in sync. If you have two entities that refer to each other using Entity Reference (or some other kind of reference field), it saves you the trouble of double-editing your entities in order to have them point at each other. If entity Alice references entity Bob, CER will make Bob reference Alice, and keep the references up-to-date when either entity is updated or deleted.

This practically means that, in your case, the show will also be referencing to the episodes and you can easily list the episodes through the content type's display options.

Notice that if you do not create a reference from the show to the episodes and you try to use views (or raw drupal database queries) in order to find the show backreferences from episodes, the generated SQL queries will probably be horrible, performance-wise.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.