Related content view with node references drupal 7

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
marieke-sa's picture

I have different content types (article, event, video, news) that have a node reference field (cat_ref), which refers to a content type Category Page, which are different kinds of sports.

So if I have an article with a node reference of surfing, I would like to display all other articles, events, videos or news that also have a node reference of surfing in a block view at the bottom.

If I create a block view with a contextual filter of my cat_ref field, I only get the intended result when adding the block on my surfing page and not on the other content types like articles nothing displays.

The contextual filter is set to content ID from url.
To specify php code in default value seems to only be connected to taxonomy terms and not node references.

Any ideas on how to do this would be appreciated.

AttachmentSize
node-ref.png126.54 KB
node-ref1.png45.12 KB

Comments

Lost post

Riaan Burger's picture

I must have lost my reply yesterday in a CAPTHCA fail.

Essentially, if I remember correctly, I suggested two (of many) options.

Context module will set you on the road for a very well structured site like this. It's well worth exploring.

Alternatively you can use a views hook to inject the argument you want, perhaps grabbing it from an arg(). This is the quick and get-it-done solution.

Please, anyone with more elegant or other options, do mention them too!

Why it's not working

foxtrotcharlie's picture

I think what the view requires for that contextual filter is the id of the Category Page, and you are supplying it with the id of the node on which the block appears, which is why it works when on the Surfing page, but it won't work on the others. You will need a way to find out what category the current item belongs to and then make this available to views to use to as the argument for the contextual filter.

I hope this helps you understand why it's not working.

As Riaan suggests, Context could be the most robust and flexible solution, so check it out. Using a views hook would work too but may be too complicated depending on your experience. The quickest, way would be to use php code to provide the default argument, but only use as a last resort because it's really not ideal to store code in the database.

Using the taxonomy system may be something to look at too - set up a vocab for category pages, and then add terms for each category page. Then use these instead of nodereferences to filter.

Thank you

marieke-sa's picture

Thank you very much for the suggestions. I think in the long run it would be better to switch to taxonomy. I will also have a look at Context. I just thought it was a small thing I was missing, as I had a related content view doing exactly this in Drupal 6 - didn't think it would be such a mission... Thanks again for the help!

Have you tried adding a

foxtrotcharlie's picture

Have you tried adding a category page as a relationship and then using that in the context filter?

I have tried relationship -

marieke-sa's picture

I have tried relationship - didn't work. I can only choose my category ref field as relationship, not content type Category Page. I can choose relationship cat_ref reverse though - the definition sounds like what I want, but now I am not sure what to put into contextual filter to use the relationship with?

Taxo?

Max_Headroom's picture

I can't understand why you can not use taxonomy in the first place as others suggested.
In D7 Taxo terms are entities already, and you got all the tools available already to do exactly what you need. Even adding fields to vocabularies if you want extra fields to show (like an image for a term).

And, I might be mistaken, as I have not used this since D6: Blocks do not get (contextual) values from URLs. You'll need to look at other ways to do this, like with Views content panes for Panels, which is what I do.

Quentin

Found the answer

marieke-sa's picture

I found the answer here:

http://www.sandstormdesign.com/blog/relationships-and-contextual-filters...

Pulling fields of “Sibling” nodes by node reference:
At first glance this may seem simple. This is a behavior usually associated with Taxonomy (i.e. “related content”), and there are a lot of built-in solutions for this kind of View when working with taxonomies. What if the relationship is defined by something else, such as in our example, Node Reference? Then you need to build it from scratch.
First, you need to ask, “what is this node referencing?” then “who else references this?”

For example:
Oranges, Banana and Mango all have the Fruit node selected in the Node Reference field. So, we would like Bananas and Mangos to show up in the sidebar when you’re on the Oranges page.

Here’s what you do:
Relationships:
1. Add relationship: choose your node reference field
2. Add relationship: same node reference field, this time “reverse”. From “Relationship” dropdown choose first relationship. Add reverse to identifier.

Contextual filter:
Add a standard Content: Nid, and provide the default value “Content ID From URL”. From the “Relationship” dropdown, choose the second relationship you added.

Fields:
Just add your fields normally! – “Do not use relationship”

Very good explanation with screen shots. Thanks Drew!

Thanks so much!

Riaan Burger's picture

Thank you very much for sharing the solution too!

Thanks for posting your

joegl's picture

Thanks for posting your solution! Helped me out a lot today!

Solution not working

bsetzer's picture

I have 2 content types - Gallery Article and Contributor. On the Gallery Article content type, I have a node reference field for Contributor.

I am attempting to create a block to place on the Gallery Article page that will pull in the appropriate Contributor content. I was sure that the solution posted by marieke-sa would work as it seems like exactly what I am trying to do, but no Contributor appears in the block when I add the various relationships and context filter.

Any thoughts on this are much appreciated... perhaps what I am trying to do is not what is described in the solution?

Thanks!

Following

nithinkolekar's picture

Following