1
votes

I am having a problem with Drupal 7 Views relationships/contextual filters.

Let me explain it with an example. I want to make a photo album.

I'm having a content type 'cat'. This content type has some fields like name, weight, etc.

I'm having a second content type 'photos'. This content type has an image field and a field named 'cats'. The field 'cats' is an entity reference to one or more nodes of type 'cat'. (There can be multiple cats on one picture).

On the node view of type 'cat' I want to show a views block with all photos that reference the current node.

And this is where I'm stuck.

I tried a multitude of combinations of relationships and contextual filters. Mostly my view didn't bring up any photos or photos not referencing the nodes.

It would be a great help for me if someone could explain me how to construct my view.

Thank you.

1

1 Answers

2
votes

You need to select your relationship to be:

Content-<cat-reference-field-name> - reverse

There should be an option like that in your 'Relationships'. Just make sure you select the cat field and the '- reverse' option.

enter image description here

Second, you need to add a 'Contextual filter'. Set that to be 'Content: cat-field', and set the default value to be 'Content ID from URL'. Up at the top, make sure to select the already created relationship from the drop down list:

enter image description here

Finally - on the fields of your image that you want to appear in the 'Fields' section, but select the created relationship for those as well (just like you did for the Contextual filter)!

Save, and if you've placed this block on a cat page, it should work. Let us know if it does!