I wanted to be able to give users the ability to create a custom filter of various taxonomy data on the fly. I know that there are modules out there that sort of do this, however, nothing quite met my needs.
So this is two parts:
1) a function that returns lists of the terms under a vocabulary (you can feed it more than one vocabulary) with check boxes that a user can check off for what terms they want to display (in an "and" capacity)
2) a javascript function that checks the form that the above code produces and redirects the user to the page
// this function returns a list navigation options function get_navigation($vocab_ids) { $html = "\n"; return $html; }
so if we do something like:
$terms = array(1,2); print get_navigation($terms);
we'll produce a nice list of terms with checkboxes next to them.
Now we just need this javascript to tie it together.
// this is used to build a filter function build_filter(field){ var terms; var base; base = '/taxonomy/term/'; for (i = 0; i < field.length; i++) if (field[i].checked == true) { //alert(field[i].value); if (terms == undefined) {terms = field[i].value;} else {terms += "+"; terms += field[i].value; } } //alert(terms); if (terms != undefined) { window.location=base+terms; } else { alert ('Please select some options!'); } }
When the form is submitted, the javascript is called, it walks through the form, picking up the ids of each of the terms, and then it build a url. If there are no terms selected, it alerts the user.



Post new comment