Community Documentation

Taxonomy Drop Down Jump Menu using Vocabulary and Terms for Block or Page

Last updated May 17, 2009. Created by karldied on October 29, 2006.
Edited by bekasu, Michelle, colorado. Log in to edit this page.

Taxonomy Drop Down Select Jump Menu using Vocabulary and Terms for Navigation of Categories in a Block or Page

Here is a working 4.7.x version of a Taxonomy Select Menu snippet that you can use for a block, on a page, or wherever you can put some PHP on your site. Only 4 or 5 things you need to change in the code below:

1) Where it says $vid=1;, replace the number 1 with the vocabulary ID number for the vocabulary you want to use to populate the drop-down with the terms in that vocabulary.

2) Where it says $formname="Company";, replace the word Company with the word(s) you want your users to see as the default text in the drop-down after List by.

3) Optional: Where it says $options[] = t('List by ' . $formname);, replace List by with the word(s) you want at the beginning of the default text in the drop-down.

4) Where it says $options['http://www.example.com/taxonomy/term/'.$term->tid] = $term->name;, replace example.com with the URL of your drupal website.

5) Where it says return drupal_get_form('company_dropdown', $form);, replace the word company with the same word you used in step 2) above - these two text strings must match.

<?php
$vid
=1;$formname="Company";

$vocabulary = db_query("SELECT term_data.name, term_data.tid FROM term_data WHERE term_data.vid=$vid ORDER BY name");
$options[] = t('List by ' . $formname); // Initialise the country array
//Populate array with url / name
while ($term = db_fetch_object($vocabulary)) {
$options['http://www.example.com/taxonomy/term/'.$term->tid] = $term->name;
}
//Build dropdown select
//If we try to build OnChange directly it gets mangled, so put in array to confuse the forms api
$form['category'] = array(
 
'#type' => 'select',
 
'#name' => $formname,
 
'#id' => $formname,
 
'#title' => '',
 
'#default_value' => '',
 
'#options' => $options,
 
'#description' => '',
 
'#multiple' => $multiple = FALSE,
 
'#required' => $required = FALSE,
 
'#attributes' => array('onChange' => "top.location.href=document.getElementById('$formname').options[document.getElementById('$formname').selectedIndex].value"),
);
//Output the form
return drupal_get_form('Company_dropdown', $form);
?>

This is a result of the collaborative work on: http://drupal.org/node/48843 - you can see the development over there, and maybe get some questions answered. There is also a version that works with 4.6.x over there.

Drupal 5

See here: http://drupal.org/node/91924
---
"Please drupal responsibly: give as much help as you get."

About this page

Drupal version
Drupal 4.7.x, Drupal 5.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