5

So I'm trying to create a View, displayed in a Block, that will show a list of nodes that reference the same parent node as the node I'm currently in. The reference is a custom field using the References module. I'm on Drupal 7 with Views 3.

I figure I'll setup a Contextual Filter on my custom field (field_ref_photo_gallery) and I need to provide a default value via PHP code.

How should I write that PHP code in the most Drupal 7-ish way?

Should I go with something like:

return node_load($nid)->field_ref_photo_gallery['und'][0]['value'];

(I'm not even sure that would work. I just threw that together in this post)

Or is there a field API function I should be calling?

4
  • can you use taxonomies in your case? Taxonomies have a built-in "depth" option that will allow you to reference the parents
    – uwe
    Oct 24 '11 at 20:18
  • did you solve this?
    – headkit
    Aug 13 '12 at 10:10
  • Unfortunately I can't remember which direction I went with this :( I do now know that you should always use field_get_items() or field_view_field() when you want to reference a field attached to an entity.
    – JonMcL
    Aug 13 '12 at 14:20
  • There will be a contextual filter on your reference field which takes the nid in the url given that you have this block only on a particular node page.
    – SGhosh
    Jan 25 '13 at 11:20
2

You can pass currently viewing nodes id in contextual filter without using any custom code.

In contextual filter settings, under When the filter value is NOT in the URL select Provide default value option. Then there is a dropdown will show. Then select Content ID from URL. See the image:

enter image description here

1
  • I have long since forgotten why I had trouble with this question. I have since done many similar Views and your answer is the method I currently use to develop them. There may have been some sort of uniqueness to my original issue, but I've since forgotten what it was. The "Content ID from URL" choice is very powerful and very poorly labeled.
    – JonMcL
    Dec 31 '13 at 16:05
0

In views 3 the "Contextual Filters" is located in the "Advanced" field set. Adding contextual filters also known as arguments will help display the appropriate content. One may also use "Relationships" in conjunction to add more fields or filters to your view.

0

Try views_php module to add custom PHP code on views filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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