2

I have a content type of Artist. I also have a content type of Release. The Artist content type has a node reference field that points to the Release content type. This node reference field allows an Artist to have 1 or more Releases.

I created a view that displays fields from the Artist content type as a page. Let's call this view "Artist Page." Artist Page has a single argument of Node: Title and a path of artist/%. This works really well.

I would like to add another view for a block of Releases belonging to a particular Artist. I hope to display this block in the sidebar for Artist Page.

How can I form a Views query that retrieves all Releases that are referenced by an Artist?

(I am making my first non-trivial Drupal 6 site and I appreciate your patience. I am happy to RTFM but please tell me where to go!)

3 Answers 3

2

Ok, So the way to do this is with arguments and relationships.

Normally using the "Provide default argument" option would work but since you're not using the actual "artist" node you can't actually get the node id from the URL.

Also just an FYI, passing the title for a views argument is a bad idea because then the query is doing a lookup based on text instead of a numeric value such as the node id which is better on performance.

But anyways the solution to your problem is add the same argument to your block view as you did to your "artist" page view. This should be the node title argument. Now add a relation to your view. The relationship you want to add is the content: [name of node reference field] relationship.

Now you can add what ever fields to your view that you would want to include from the "release" content type but now you will see you get a relationship option in the field option. When you want to display fields from the "release" content type just be sure to select the [node reference] relationship.

Also we need to do one more thing. Which is getting the proper argument to be picked up by your block view. This is a bit trickier mainly because of what I mention earlier which is that you're passing the node title as an argument. So now you can edit your Node: Title argument and you should see the "option to take if the argument is not present" select the "provide default argument" and select the php code option. In there you want to paste this piece of code.

return arg(1);

Now save your view and place that block on the page and visit the artist/% page and you should now see what ever fields you want to use. This php code is telling your block view to pick up the artist/% argument from the url.

A couple of things to lookout for:

  • php code in the views is bad, I just mentioned that here as a quick fix to your problem.

  • When using relationship be sure to select the relationship option only when you want to display a field from the "related" item. In other works if you want to display the title of the related node select node title and be sure the relationship is selected. If you want to display the title of the node being passed in on the argument select node title just don't select any relationship.

Hope this is helpful, is a long post but relationships can be confusing.

Hope this helps.

0

Add an argument for the node reference, and choose "Provide default argument", setting that to "Node ID from URL".

4
  • Which URL? I never display "bare" Artist nodes -- just views that are referenced by the URL: artist/[artist node title] Mar 31, 2011 at 19:54
  • Hm, not sure if it'll still pick it up. Worth a try. Mar 31, 2011 at 19:57
  • Aha. Let me try that now. Mar 31, 2011 at 19:58
  • Yes, thanks, but that is not working. :( The block View has a filter of Node: Type = Release, an argument of the Releases node reference in Artist (with a default argument type of Node ID from URL), and a single field of the Release's name. When I go to an Artist Page, nothing appears in the block. Mar 31, 2011 at 20:07
0

The Artist content type has a node reference field that points to the Release content type.

That way, every time a new Release is created, an Artist must be edited. If you instead give the Release content type a node reference to the Artist content type, a Release can be created without editing an Artist.

In that case the view would have the node reference field for the artist as a relationship and a node nid argument that uses this relationship.

As ericduran explained in his answer, use a default argument to circumvent the limitation that block displays do not automatically receive arguments from the URL.

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.