Community Documentation

Taxonomy filter block (including matching of child terms in hierarchical vocabularies)

Last updated July 27, 2006. Created by adrian on July 26, 2006.
Log in to edit this page.

While teaching people how to use taxonomy at DrupalCamp Johannesburg,
I suddenly realised that Drupal doesn't filter more than one level down in taxonomy.
Although the functionality is available if you know how to use the URL, there's no interface for it.

The example was a realty site, with three vocabularies :

Location , which was a single inheritance , single select , hierarchical taxonomy of country / province / city / suburb.
Rooms , a flat, single select list containing 1 bedroom, 2 bedrooms etc.
Features, a list of things the house might have that are useful (ie: pool, big yard, near school etc).

The following block displays a form allowing you to select the criteria for matching houses, and this displays the right nodes.
It supports both multiple and single select vocabularies, but will probably need to be modified to do tag matching too =)

<?php
 
function findhouse_submit($form_id, $edit) {
   
$tid = array();
    foreach (
$edit['terms'] as $terms) {
      if (
is_array($terms)) {
       
$tid += $terms;
      }
      else {
       
$tid[$terms] = $terms;
      }
    }
   
$path = 'taxonomy/term/' . implode(',', $tid) . '/all';

   
# drupal_goto doesn't like comma characters. Turns them into their escaped equivalent.
   
drupal_goto($path);
  }

 
$form = array();
 
$form['terms']['#tree'] = TRUE;
  if ((
arg(0) == 'taxonomy') && (arg(1) == 'term')) {
   
$terms = explode(',', arg(2));
  }
  for (
$i = 1; $i <= 3; $i++) {
   
$form['terms'][$i] = taxonomy_form($i, $terms);
  }
 
$form['submit'] = array( '#type' => 'submit', '#value' => 'find my house');
  return
drupal_get_form('findhouse', $form);
?>

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 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