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->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.
code
$node=menu_get_object(); $field = field_get_items('node', $node, 'field_event_artistref'); return field_view_value('node', $node, 'field_name', $field['target_id']);code
. Here's a link I was following on the topic computerminds.co.uk/articles/…