2

There are two content types Gallery and Image. Image has node reference field (node reference from url). Gallery doesn't have any node reference fields.

By using views I can show all related images on a gallery page (I use node reference field "field_pers_gallery" as a contextual filter).

At the same time I need to create a view to show all images related to one gallery (all images have the same value in field field_pers_gallery) on a page of every image. I would like to create a view that looks like Flickr image page with related images. I have the problem with it because I cannot choose the right contextual filter to create the view as I want.

I tried the following examples but nothing helped me, I cannot understand why.

Could anybody help me with any advices?

  • 1
    Are you creating a relationship for the node reference? – Alex Gill Oct 9 '12 at 8:33
  • 1
    Yes, I choosed field_pers_gallery as a relationship. If I chose Node:nid as context filter and don't use relationship my view show me only one image that is a current image page is. If I use relationship in context filter my view doesn't show me anything – moto Oct 9 '12 at 8:37
2

I think here is a simple solution just add that the node reference field (field_pers_gallery) to contextual filter then all nodes (Images) which have same value in that field can be displayed on a page.

  • That one is the correct answer! – leymannx Jan 29 '14 at 12:05
0

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.

² 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.

  • Is there any need to use PHP code? Basing on what the OP wrote under SOLVED (the part I am going to delete), there is no need to use PHP code. – kiamlaluno Sep 3 '13 at 12:34
  • This is the only solution I was able to find after trying several different suggestions. It was the only thing that worked for me, but YMMV. – davidneedham Sep 3 '13 at 22:44
  • If you are using References module, then you need to adjust the code as the field structure is different. – mpdonadio Sep 4 '13 at 0:24
  • This example isn't using the References module - so no worries there. – davidneedham Mar 13 '15 at 19:39
0

I'm not sure about node reference, but since another answer proposes Entity Reference for the reference field(s) I will piggyback on that suggestion.

With Entity references in place, you'll be able to add "reverse references" within the relationship builder in the Views admin UI. These will read as

"Entity Reference: Referencing entity"

(as opposed to Referenced entity)...

Here's what these look like:

enter image description here

It takes a bit of getting used to, maybe some eye strain, to establish the reverse reference you're looking for. Once working, I find it helps to give it a meaningful administrative label, like "Nodes pointing here" or, in your case "Appearing in [Galleries]:" or something similar.

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.