Drupal Views: Using Term Arguments

Have you ever wanted to land on a node and have a block appear showing another node(s) that shares the same vocabulary term? This is easy enough if you only have a small predictable number of nodes and terms. You could simply create a series of view blocks that show all nodes with the term you want and then assign the block to the specific node. But, what if you wanted this block process to work when there was an undetermined number of nodes and terms? And what if you didn't want your client to have to create a block (manually or via a view) and assign the block each time a term was added? How would you simplify or somewhat automate the process? Enough of the generic talk, here is my specific scenario and the solution I used.
When I create a site, I like to use a vocabulary that I call Sections. The terms in this vocabulary are the names of the site sections that I want to appear in the URL when I use Pathauto. When I create a node, I assign the node to a section term. This gives my node a complete URL like http://sitename.com/sectionname/title-of-node. In one site I built, I wanted a section image to appear on a node page based on the section it was assigned. I couldn't predict the number of nodes in the section and the number of section terms was more than just a few and more could be added. I didn't want to create a unique block for every term, it just seemed like I would be making way too many blocks. Why not create a view block that says "show the image associated with an image node if the image node and the node being shown share the same section term?" This is how I did it.
I created a view block and assigned the block to the sidebar region of my choice by doing the following.

  1. I created a content type called image and added the CCK imagefield to hold the image. This is important because a view needs a "field" to reference and if you want to control the size of the image using imagecache, you need the imagefield.
  2. I added the Section vocabulary to the image content type. Note: you need to set the Section vocabulary to a weight of -10 so that it will always be the first vocabulary on the node. This is needed for Pathauto to work properly and so you can consistently compare the same vocabularies between nodes.
  3. I created a view with the following settings.
    1. Filter => Node: Type = Image
    2. Field => Content: Image and I assigned an imagecache preset that resized the image so it would in the sidebar area.
    3. Argument => Taxonomy: term ID. I set "Action to take if argument is not present:" to be "Action to take if argument is not present: " and the default argument type to be PHP code. The code snippet I used came from here http://drupal.org/node/503184#comment-1745936 (thank you binford2k)
      foreach($node->taxonomy as $term{
      if ($term->vid == 2) {
      if ($terms) { return implode('+',$terms); }
    4. Display => I added the block display to the view.
  4. Then I added the block to the side bar.

If you want to see this working, you can go to http://www.thelegalbalance.com/events/about-our-events to see one of the pages working. This is a fee-based community so you won't see all the pages that are using this feature. I didn't make the theming decisions for this site, I simply built it.