Custom Filter

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"; // first we want to loop through each of the terms being passed in foreach ($vocab_ids as $vocab_id) { $term_title = taxonomy_get_vocabularies('flexinode-1', $vocab_id); $html .= "

" .$term_title[$vocab_id]->name . "

\n"; $html .= "
    \n"; // now loop through each single term foreach(taxonomy_get_tree($vocab_id, $parent = 0, $depth = -1, $max_depth = NULL) as $term) { // make sure there are items in this $the_count = taxonomy_term_count_nodes($term->tid); if ($the_count > 0) { $html .= "
  • tid . "\">" . $term->name . " \n"; $html .= "tid . "\" />\n"; $html .= "
  • \n"; } } $html .= "
\n"; $html .= "
\n"; } // end foreach $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

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <pre>
  • Lines and paragraphs break automatically.

More information about formatting options