1

I have a content type on my Drupal 7 site which has a field called field_related, with a type of Node reference (I've installed References). This allows me to select nodes that are related to the one I am editing. I'd like to then display these in a view, but am coming unstuck with the setting up of the view.

I've set up a single document with some related nodes for testing. If I set a relationship up in my view as "Content: Related Nodes (field_related) - reverse", I see a list of the related nodes I defined. But I see these same related nodes on every single page - not just the page they are defined for. ("Require this relationship" is ticked.)

If I set up the relationship as "Content: Related Nodes", nothing appears. I've tried a variety of filters, contextual filters and relationships, but that hasn't helped much.

I'm sure I'm missing something obvious ... can anyone enlighten me?

Some screenshots of the current setup, so you can see what I've got so far:

  • View

    screenshot

  • Contextual filter

    screenshot

  • Relationship

    screenshot

4 Answers 4

3

Got there in the end, thanks to a combination of the (very nice and helpful) Baysaa and keva. This was the method that got there in the end:

  • Contextual Filter: Content: Nid (set to "do not use a relationship"; default value of "content ID from URL")
  • Relationship: Content: Related Nodes (with "require this field")
  • Then, I went to Format and changed Show: Content to anything else. Then I changed it back to Show: Content - this allowed me to select the relationship I defined above (I couldn't define the relationship here without changing Show: Content to something else first).
1
  • Anyone who is looking for solution to use this in Show: Fields: In field settings (for example Content: Title) you can define Relationship from dropdown.
    – hene
    Mar 3, 2016 at 9:52
2

On the settings of the "Content: Related Nodes" contextual filter, under "WHEN THE FILTER VALUE IS NOT AVAILABLE" have you selected "Provide default value" and set "Content ID from URL"?

EDIT: Sorry I meant under the "Contextual Filter". "Content: Related Nodes" should be a Contextual Filter, not a relationship.

7
  • I've added that, but it hasn't changed anything. I've added some screenshots to the above for clarity.
    – Dave Child
    Mar 15, 2012 at 15:06
  • Remove the Relationship from the Contextual filter, and remove the Relationship altogether.
    – Beebee
    Mar 15, 2012 at 15:07
  • Getting nearer - thanks :). I've removed the relationship and contextual filter, and added a new contextual filter as you described. Unfortunately, it seems to be working in reverse. Instead of looking at my test node (A) and seeing the three related nodes I defined for it (B, C and D), I now see node A listed as a related node on each of B, C and D.
    – Dave Child
    Mar 15, 2012 at 15:13
  • What's the result of the view when you view Node A? Did you by chance install a module called "Corresponding Node References"?
    – Beebee
    Mar 15, 2012 at 15:18
  • On Node A, I see none of the referenced nodes. And no, no module by that name.
    – Dave Child
    Mar 15, 2012 at 15:21
2

It would help to know how you will be using the view. If you just want to show the related nodes (B,C,D) on node A - say in a sidebar block - then your view is getting over-complicated.

Remove the Contextual filters and Relationships you have, then ...

Add field:

  • Content: Related Nodes
  • Choose a formatter

Add Contextual Filter:

  • NID
  • WHEN THE FILTER VALUE IS NOT AVAILABLE > Provide default value > Content ID from URL
1
  • Thanks - this was almost there. It gave me the list of related nodes, but I couldn't format them the way I needed to. I did manage to sort this out using a relationship, contextual filter and by applying a relationship to the format block.
    – Dave Child
    Mar 16, 2012 at 10:30
0

Contextual Filter: Content: Nid select >> Provide Default value >> PHP code

As we will be displaying related nodes block on a node view page for which we want to display related nodes. So we will load that node first and get all the related nids. Then we return the nids in string format like below '56 + 32 + 112'

$nid = arg(1);
$node = node_load($nid);
$str = "";
$related_nids = $node->field_related_nodes[$node->language];

if(is_array($related_nids) && sizeof($related_nids)){
  foreach($related_nids as $related_nid){
    $str .= ($str)? '+' .  $blog['nid'] :$blog['nid'];
  }
}
return $str;

Also click the More link in contextual filter and check "Allow multiple values"enter code here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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