2

Let's say I have homes and neighborhoods (homes reference the neighborhood they're in via a node reference field). On the home detail page I want to show other homes in the same neighborhood.

Seems simple enough, and I've found this: Create a view on a node reference field but it is not quite what I need to do, since I need to find other homes that reference the same neighborhood as the current home does (not ones that reference the current home).

I also found this: Node references view: get other nodes that also reference the same node but I am only using 2 content types (homes and neighborhoods). Any ideas? My brain is tired after trying to think through the relationships and arguments of this thing :)

4 Answers 4

2

You should be able to do this with a relationship. Add the neighborhood node reference field as the relationship. Add NID as your argument and apply the neighborhood relationship that you just created as the relationship to that NID argument. This will tell views to use the NID of the node reference field as the argument.

1
  • Best answer for sure
    – ipwa
    Aug 13, 2012 at 9:43
1

What you are doing is commonly called a reverse node reference. You are querying to find all the nodes that have a node reference field referring to the current node. Here's how to do this:

  1. Add the node reference field as an argument to the view.
  2. Add $args[0] = arg(1); as the argument handling code.

Its important to note that you will create a block display for the view so that the arguments will get evaluated properly.

1
  • Nope. I wish it were that simple. In fact, that's what the first link I posted explains how to build. But my situation requires a block that displays nodes that reference the same node the current node references, not nodes that reference the current node. In my situation, your solution would be more suited for a neighborhood detail page that lists all the homes in that neighborhood, as opposed to a home detail page that lists all the other homes in that same neighborhood. Sep 27, 2011 at 0:24
1

The easiest way is to use Viewfield module.

  1. First, install Token and Viewfield
  2. Add new view for home content type with two arguments (contextual filters):
    • Your reference field to neighborhoods
    • Content:Nid with default content id from URL and check «Exclude». Optional, to exclude node which is viewed.
      enter image description here
  3. Go to your home content type and add new field of type Views.
  4. Configure field with tokens help. enter image description here
    • NOTE: I don't know how this will work with multiple references, but with single reference value it works great. Anyway you will be able to solve it by PHP Validate code in Views.

Update: Relationships are better choice in this task, but I found strange bug on my Drupal with it so I suggested way with Viewfield.

0
0

Hopefully this will help someone...

Taken from https://drupal.org/node/131547 Exclude the current node from a list view.

Views 3

  1. Click on the "advanced" tab.
  2. Click on add under "contextual filters".
  3. Choose Content:nid.
  4. Under "when the filter variable is not available, choose "provide default value".
  5. From the drop down menu select "content id from url".
  6. Scroll all the way down to the bottom of the window and click on the "More" link.
  7. Click "Exclude"

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.