Community Documentation

Related block by leaf term(s)

Last updated July 14, 2010. Created by hexblot on July 14, 2010.
Log in to edit this page.

After much digging around, I didn't find anything that worked for Drupal 6, so I implemented my own, based on other snippets found.

This is an example about how to create a view that generates a block of nodes that have the same leaf taxonomy term as the main node being displayed.

Example:

  • you have a tree vocabulary with id 4
  • it has items A,B and C, and C has a child term D
  • you are viewing node/123 ( or an alias of it )
  • it has the terms A, C and D applied to it
  • the block will display items that have A or D applied to them if you select OR, or A and D if you select AND

Tested on Drupal 6.17 using Views 6.x-2.11.

Follow this process to achieve your desired result:

  • Create a view as normal ( define filters, sort order, items to return, style, etc)
  • Add a block display
  • Add an argument of type "Taxonomy: Term ID".
    • Choose "Provide default argument"
    • Choose "PHP Code"
    • Enter the following code, after after removing the PHP opening/closing tags, and replacing the first values if needed:

      <?php
      // DO NOT FORGET TO REMOVE THE OPENING / CLOSING PHP TAGS!!!!

      // Use this snippet as argument handling code.

      // Change the following to match your needs
      $vid          = 4;       // "Interesting" vocabulary ID
      $node_type    = "story"; // Node type, as in machine name
      $tid_op       = "+";     // Enter "+" for OR, "," for AND

      //********************************************
      // You don't need to tinker below this line
      $tids = array();
      $node = menu_get_object();

      if ( !empty(
      $node) && $node->type == $node_type && !empty($node->taxonomy) ) {
          foreach(
      $node->taxonomy as $term ) {
              if (
      $term->vid == $vid ) {
                 
      // Interesting term, need to find if it's childless
                  // (Fortunately, this query is cached by Drupal, so it's not a big
                  //   performance hit)
                 
      $term_children = _taxonomy_term_children($term->tid);
                  if ( !
      count($term_children) ) {
             
      $tids[] = $term->tid;
            }
      // if is leaf
         
      } // if vid is interesting
       
      } // foreach
      } // if on an interesting node
       
      return implode($tid_op, $tids);


      // DO NOT FORGET TO REMOVE THE OPENING / CLOSING PHP TAGS!!!!
      ?>
  • This should provide nodes of the same leaf term ID, but by default, that can also include the node you are currently viewing (after all, it also has this term! ). Solution:
    • Add another argument of type "Node: NID"
    • Choose "Provide default argument"
    • Choose "Node ID from URL"
    • Finally, check the "Exclude the argument" box near the bottom

Please let me know how things work out for you :)

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers, Site users

Reference

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here