Skip to main content
added 1030 characters in body
Source Link
Will
  • 302
  • 1
  • 2
  • 6

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

      band content type has some basic fields
      song content type has a reference field referencing which band
      event content type has a reference field referencing which band is headlining
      view has a block with fields
  1. artist content type has some basic fields
  2. song content type has a reference field referencing which artist
  3. event content type has a reference field referencing which artist is headlining
  4. view has a block with fields

on each BandArtist content page, it's working and i have a block of songs for that bandartist 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 bandartist in the sidebar. Essentially i want the reference field in the event to match up with the band'sartist'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.

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

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

on each Band content page, it's working and i have a block of songs for that band 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 band in the sidebar. Essentially i want the reference field in the event to match up with the band'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.

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

  1. artist content type has some basic fields
  2. song content type has a reference field referencing which artist
  3. event content type has a reference field referencing which artist is headlining
  4. 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.

Source Link
Will
  • 302
  • 1
  • 2
  • 6

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

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

on each Band content page, it's working and i have a block of songs for that band 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 band in the sidebar. Essentially i want the reference field in the event to match up with the band'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.