2

I have job nodes, that relate to organizations. I want a sidebar block that will display other jobs that are related to the same organization.

I have tried a lot of different permutation of contextual filters and relationships, but so far I have not been successful.

I can only get the nid of the current node via a contextual filter, but that filters the content directly. I need just to get the referenced organization node, and then filter the view by that reference, but I can't seem to make it work.

(This question is for Drupal 7 with entity reference. The same question for Drupal 6 is here).

3 Answers 3

3

I found another (better) solution which doesn't require writing PHP if you're referencing taxonomy terms.

  1. Make sure you're filtering the view by the content type you want to view a list of, not the one you're trying to reference.
  2. Add a contextual filter using the entity reference field.
  3. Select 'Provide default value' and select 'Taxonomy term ID from URL'.
  4. Check the box for 'Load default filter from node page, that's good for related taxonomy blocks', along with whatever other configuration you want.
  5. Save the view and check it out.
3

Here is a solution using the Entity Reference module to display other nodes that reference the same node that the current node references. No need for relationships, just follow these steps:

  1. Make sure you're filtering the view by the content type you want to view a list of, not the one you're trying to reference.
  2. Add a contextual filter using the entity reference field.
  3. For the entity reference field, choose to provide a default value in PHP ¹.
  4. Adapt the following code to your field names ² and save the contextual filter:

    $node = menu_get_object();
    $referenced_node = $node->field_YOUR_FIELD_NAME['und'][0]['target_id'];
    return $referenced_node;

  5. Save the view and check it out.

¹Disclaimer: PHP should never be used in the UI due to concerns with security, performance, & lack of version control. As a result, the proper way to implement this solution involves writing your own simple module that creates a block that passes the argument into the view as you render it in the block. I'm still including this because whether people ignore this warning or use it to write a module, hopefully it helped.

EDIT: If you also want to exclude the current node, you'll need to add an additional contextual filter for NID and make sure to select the "exclude" option.

² Not sure what your field names are? Install and enable the Devel module and visit a node. Click the 'devel' tab and navigate through the list to find your desired entity reference field.

1
  • Had an email from someone asking about excluding the current node the view is appearing on, so I went ahead and edited this answer to include information about that. Apr 8, 2016 at 17:05
1

Try selecting the relationship on the contextual filter.

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.