1

I have a content type 'property' and a content type 'park'. I'm using an Entity Reference field in the property to reference a park. The park is essentially a parent or neighborhood or that contains the property.

On the property page, I want to display the sibling properties (the ones that share a common park/parent). I'm able to show child properties on the park page, but can't for the life of me figure out the relationship/contextual filter combo to use to get this working. I see many examples online that use taxonomy, but this is an entity reference relationship. Setting up a relationship I have options for Entity Reference: Referenced Entity - A bridge to the Content entity that is referenced via field_master_park and Entity Reference: Referencing entity - A bridge to the Content entity that is referencing Content via field_master_park and I assume I use (at least) one of these combined with a contextual filter?

Anyone able to at least point me in the right direction?

1 Answer 1

3

There are two ways to do this that come to mind:

Option One

You could create a node template for nodes of type property and use an Entity Field Query in a manner roughly following this pseudo-code:

  $park = $node->field_park['value'];
  $query = new EntityFieldQuery();
  $query
     ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'property')
    ->propertyCondition('status', 1)
    ->propertyOrderBy('created', 'DESC')
    ->fieldCondition('field_park', $park)
    ->range(0,10);
$result = $query->execute();

This will load a list the NIDs of all nodes that have field_park matching the value of $node->field_park['value'] (or wherever the node object stores your actual field value). To get more info than just the NID, you will want to do some kind of foreach($result) that does a node_load($nid) on each NID returned by the EFQ.

Option Two

Option two is no-code: you could create a View and embed it on your node with something like Insert View. You would create the view with the following steps:

  1. Add a contextual filter that takes the node ID. This is so you can pass it a value so you get a view specific to each node you want it to appear on. Exactly how to do this is out of the scope of this question.
  2. Add a field for the node title. You can remove this later but it will keep you from getting confused while making the view. Give it a label if you want, make it grouping field number one, and exclude it from the view.
  3. Add a relationship. Choose "Entity Reference" for the relationship type, and then select "entity that is referenced via field_park" or whatever your node reference field is called.
  4. Add another content: title field. Under relationship, choose the relationship you just created. Set it to group by that field, create a label called "park:", and set it to exclude from display.
  5. Add another relationship. Choose type "Entity Reference" and this time choose "referencing entity": "entity that is referencing Content via field_park"
  6. Add another field for node: title and this time select the relationship you just created. Give it a label like, "sister property:" if you want.

Violá. Now you have a view that will allow you to pass an NID and will tell you the title of that property, its park, and all sister properties.

5
  • Hey Beth, thanks for this! I'm working through it now.
    – circlecube
    Commented Nov 21, 2012 at 13:21
  • This is all nice, but the last couple steps (where I was fuzzy) is still pretty brief. But it does reassure me that I'm on the right path. Step 5: Missing detail that I needed to select the other relationship when setting it up. If not then it returns nothing. I was trying out the different options and finally came to reason through the referencing entities should be in relation to the referenced entity. Thinking that though is easier than putting it into actionable words though... Step 6: add the field for content:title (I know, essentially the same, but trying to be consistent with the UI)
    – circlecube
    Commented Nov 21, 2012 at 13:49
  • I'm not sure I understand your question.
    – beth
    Commented Nov 21, 2012 at 15:18
  • Hey- it wasn't a question, only clarifying details to the last couple steps in case someone tries to follow along
    – circlecube
    Commented Nov 29, 2012 at 13:56
  • I stumbled upon this while searching for the same answer, so I thought to add: I found a great explanation for how to display entity reference siblings: Relationships and Contextual Filters in Views 3 for Drupal, it's the last case in the article.
    – prkos
    Commented May 10, 2018 at 0:53

Your Answer

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

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