Hi there.

I have created the Nodetype "Animals". Furthermore there is a voacabulary "Species" with taxonomy terms. This vocabulary is used by all content types.

What i want is, when you watch a node from the type "Animals" that there is a block which shows all nodes which belongs to the same taxonomy term.

I dont want to create a new view for every term. i think its a question of arguments. hope you can help me!

Comments

smacphail’s picture

Status: Fixed » Active

Yup, it will come down to using arguements. I haven't been using Views very long, and I'm an intermediate PHP developer, (so pros, please chime in and correct/improve my suggestion) but this is the solution that worked me:

1. you'll basically want to set up an argument on Term ID (or Term Name).
2. Under "Action to take...", choose "Provide default argument"
3. Under "Default argument type", use "PHP Code"
4. Views doesn't know about your $node, which is where your taxonomy term for "Species" is set, so you need to load $node.

$node=menu_get_object();

menu_get_object() in this case is the equivalent of going node_load(args[1]). I believe it's a new function to D6.

Now that you've got your $node object, you'll need to loop through its taxonomy. (NOTE: this assumes that your custom content type "Animals" has only *one* vocabulary assigned to it ("Species") and that the vocabulary can only have one value.)

foreach ($node->taxonomy as $tax_term_obj) {
   // We're assuming there's only one item in the taxonomy, the value for Species; we have to loop because
   // the array is a key array, so we can't just ask for $node->taxonomy[0]
   return $tax_term_obj->tid;
   break;
}

That will return the very first term id in your taxonomy object, so then Views will then look for Animals that are tagged with the same species.

If you have a few different vocabularies assigned to your content type, either make sure Species is the "heaviest" vocabulary (that means its value will always be first, above the others in the taxonomy array), or you'll have to get Species vocabulary ID first (taxonomy_get_vocabularies();), and then loop through the entire taxonomy object, looking to see where the taxonomy term you're looking at has a vocabulary ID that matches up with Species.

So, the full version of this whole argument would look something like this:

// Get the node...
if ($node = menu_get_object()) {
      // Get all our vocabularies...
      $vocabs = taxonomy_get_vocabularies();
      $tax_vid = 0;
      // Loop through and find the one called "Species", and get its vid (Vocabulary ID)...
      foreach ($vocabs as $vocab_item) {
        if ($vocab_item->name == "Species") {
          $tax_vid = $vocab_item->vid;
          break;
        }
      }

      // Great, now lets get the value of Species for this piece of content...
      $term_id = 0;
      // Loop through the node's taxonomy array...
      foreach ($node->taxonomy as $tax_term_obj) {
        // Does the term we're looking at belong to the Species vocabulary?
        if ($tax_term_obj->vid == $tax_vid) {
          // It does, so assign this current term's ID to our var...
          $term_id = $tax_term_obj->tid;
        }
      }
      // ...and then return it back to Views
      return $term_id;
    }

Yea, it's a little long, but I'm not sure just yet how it can be done any other way/improved.

BTW, if you *also* want to make sure that you don't load in the Animal that you're currently looking at into your View, set up another argument for Node ID, do the same thing (Provide default, PHP code, etc.) and the argument will be

return args[1];

That will return the ID of your node. Then scroll to the bottom of the Views argument options and make sure to "Exclude" this item from the result set.

Now your Views will show other Animals with the same Species and will exclude the one you're currently looking at. Unless I made a mistake above. :P

Good luck!

Witch’s picture

Hi smacphail!

I solved this issue by this code

if ( arg(0) ==  'node' && is_numeric(arg(1)) )  {
  $node = node_load(arg(1));
  if ( is_array($node->taxonomy) ) {
    $wanted_vid = 4;
    foreach ( $node->taxonomy as $term ) {
       if  (  $term->vid  == $wanted_vid  )  {
         return $term->tid;
       }
    }
  }
}

thank you for trying to help me!

smacphail’s picture

Status: Active » Fixed

Excellent! Glad you were able to sort it out. The only "problem" is that your vocabulary id hard coded to "4", but if you never expect that to change or you're not going to move to another server or whatever, then you're fine. It saves you one less query of the database.

EDIT - I made a mistake in my code, with the statement where I'm looping through the vocabularies.

if ($vocab_item->name == $vocab_name) {

I didn't define $vocab_name anywhere, so you should just change that to == "Species" or whatever.

Status: Active » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.