David Lohmeyer's Blog

CCK field as a view argument in Drupal 6

I found this trick during the development of Acton Institute's new Drupal-ized website.  I wanted to be able to list out other articles by the same author and show it on a sidebar.  Here's the setup:

  • Article authors are CCK nodereference fields to a separate author content type

How does one then construct a view that thinks the author is a CCK field and not the actual node's author?  It's a lot easier than I thought.  The only thing you need to have in your view is an argument with a little PHP magic.

  • Create an argument and locate your author field attached to the article type you'll be showing this view on (I do it with block visibility)
  • Select "Provide default argument"
  • Under "Default argument type" select "PHP Code"
  • Paste this in the box, replacing field_author with the machine-code name of your field:
if($node) { 
     return $node->field_author[0]['nid']; 
} else { 
     return FALSE; 
  • I have Action to take if argument does not validate set to "Basic validation" and "Action to take if argument does not validate" set to "Hide View / Page not found (404)"
  • "Allow multiple terms per argument" is checked.
  • "Exclude the argument" is unchecked.

I then added a block and set it to only show on certain articles.  When visitors go to an article they can see the latest articles by the author of the article they're viewing.

Related Posts