1

I have a content type: "artist_news" which has an entity reference to "field_artists". So that when you create a news story you attach it to an artist.

the url for this might look like this: /artists/picasso/news/my-news-item

I am trying to create a view block, to appear on the page "/artists/picasso/news/" that shows all news "artist_news" items that reference any given artist (e.g. picasso)

I cannot figure out how to exactly structure it. I suspect I need to use contextual filters but the exact settings of contextual filters is escaping me.

Thank you in advance for your help.

  • As noted in the comment below: I've added a contextual filter: on the Content: artists and a default value of Content ID from URL and this does not work. Keep in mind, my view is filtering on a different content type: artist_news and that content type has an entity reference to field_artist. Artist comes from the URL (artists/picasso/news) The method describe does not produce results. Maybe I am missing something? Thank you for any assistance in explaining this. – Scott Edmonds Mar 8 '13 at 22:37
0

That is just your pretty URL. Your actual url is something like this node/52.

So in your contextual filters, choose provide default argument and then select the content id from the URL. Views will pull in the 52 number for your referenced entity and that will filter your block accordingly.

UPDATE Since you are manually creating your URLs, and idea here would be to grab the first and second element from your path. I do not recommend this. I would recommend you adding another field to reference the artist on the news basic page so you can use that value in your contextual filter. Anyway here is what you could do.

In the contextual filter, when providing a default value, use the PHP filter and add something like this to return the value of the NID of the artist.

$path = drupal_get_path_alias($_GET[‘q’]);
$path = explode(‘/’, $path);
// This path should be something like artists/picasso
// The idea here would be to get the artists node path
// and then retrieve the node id from that.
$node_artist_path = drupal_get_normal_path($path[0].'/'.$path[1]);
// ^ should return something like node/33
$nid = arg(1,$node_artist_path);
return $nid;

This will return the value of nid from the artists main path. AGAIN I do not recommend this as its very dependent on path aliases.

Also I did not test any of this code, I may have some functions wrong and if I do let me know and I will update it, this is all from memory.

|improve this answer|||||
  • Thank you for your reply, however, if you could be more explicit it would help. I've added a contextual filter: on the Content: artists and a default value of Content ID from URL and this does not work. Keep in mind, my view is filtering on a different content type: artist_news and that content type has an entity reference to field_artist. Artist comes from the URL (artists/picasso/news) The method describe does not produce results. Maybe I am missing something? Thank you again. – Scott Edmonds Mar 8 '13 at 19:04
  • What type of page is /artists/picasso/news? Meaning how did you create that URL? – iLLin Mar 9 '13 at 13:52
  • /news is a basic page whose URL was created using pathauto, that is where I will display my view block. – Scott Edmonds Mar 10 '13 at 14:45
  • Hmm, you might want to rethink your architecture so you are going with the grain instead of having to create a custom function to fetch your UID. Does your basic page have a reference to the artist? Is that how pathauto is creating the link to the artist name? If thats the case, you can use that reference field as your contextual filter. – iLLin Mar 11 '13 at 15:34
  • Thank you for your help, but I'd rather know if it is possible to solve the problem using the structure I have, rather than find a different way. If it is not possible, then I can restructure. Each item in the view (news item) is linked to artist already (entity reference to field_artists) so I don't want to have to have another reference on the /news/ page. This page is supposed to act like a taxonomy display page, showing all news items that are linked to the artist. I don't understand why it's not possible to filter the returned news item by the referenced artist, if artist value is in URL. – Scott Edmonds Mar 11 '13 at 16:07

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.