views and contextual filters with entity reference field how do you use contextual filters to a views block when you want to use a reference field from the content type displaying the view?

here's the simple scenario


artist content type has some basic fields
song content type has a reference field referencing which artist
event content type has a reference field referencing which artist is headlining
view has a block with fields


on each Artist content page, it's working and i have a block of songs for that artist in the sidebar. i used views provide default entity and basic validation (but does not work for below)

on each Event content page, i want a block of songs for that artist in the sidebar. Essentially i want the reference field in the event to match up with the artist's songs.

since we don't have access to the path, i've been playing around with contextual filters on the reference field (and reverse of it) to no avail.



Ok thanks to @Jimajamma I seem to have figured it out, it's actually quite simple.

In order to get the argument into the Song view, you add a 'contextual filter' and select the Song's artist reference field. Then under 'When the filter value is NOT available' choose 'provide default value' and PHP Code. In that PHP code you want to return the value of the Event's artist reference field like so

$node=menu_get_object();
return $node-&gt;field_event_artistref['und'][0]['target_id'];


then under 'When the filter value IS available or a default is provided', choose 'Specify validation criteria' and basic validation

In a nutshell, this is simply taking the argument of the artist the Song references and comparing it to the default value you provided which is the current node's artist reference field. also as @Jimajamma said, best practice would be to use a switch statement on the node->type to be able to use the songs by %artist block on several different types of content like artist nodes.
In the contextual filter, you can use php to supply the argument.  There, you could put in something like:

$node=menu_get_object();
return $node-&gt;field_headliner[0]['nid'];


where field_headliner is the node reference field of your event content type.  The exact syntax above is D6, but I am pretty sure it's the same or at least very similar in D7.

Then, just as long as this view block is being show in/on an event page, the above will return the nid of the headlining band, and if your view is set up to filter on that node nid, you should be set.

You could even get fancy up in there with something like:

$node=menu_get_object();

switch ($node-&gt;type) {
  case 'band':
    return $node-&gt;nid;
  case 'event':
    return $node-&gt;field_headliner[0]['nid'];
  default:
    return 0; // or however you want to return an error
}


and then the same view block display could be used on both types of page.  Then just put this block on band and event pages and you should be all set.