Drupal 5.x
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" (line 5);, 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. Same changes in lines 2 and 4.
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, or, leave only '/taxonomy/term/' to user relative path.
$output = drupal_get_form('Company_dropdown_form', $form);
return $output;
function Company_dropdown_form() {
$vid=1;
$formname="Company";
$vocabulary = db_query("SELECT td.name, td.tid FROM {term_data} td WHERE td.vid=%d ORDER BY td.name", $vid);
// Initialise the country array
$options[] = t('List by ' . $formname);
//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"),
);
return $form;
}
Drupal 4.7.x
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.
$vid=1;
$formname="Company";
$vocabulary = db_query("SELECT td.name, td.tid FROM {term_data} td WHERE td.vid=%d ORDER BY td.name", $vid);
// Initialise the country array
$options[] = t('List by ' . $formname);
//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.
Extras
Ordering character variables like numeric ones
If you find that you have categories like 1, 10, 22, 2, 210 (extreme example) you can organize them as if they were numbers (i.e., a jump-menu like this: 1, 2, 10, 22, 210) rather than like text (i.e., a jump-menu like this: 1, 10, 2, 210, 22) by including the following in the jump-menu's SQL query:
db_query("SELECT td.name, td.tid FROM {term_data} td WHERE td.vid=%d ORDER BY LENGTH(td.name), td.name ASC", $vid);
---
"Please drupal responsibly: give as much help as you get."
Comments
This worked pretty nicely
This worked pretty nicely for me so thanks kindly for taking the time to put it together.
When I use my code below, I notice that the paths returned are something like:
"taxonomy/term/45"
I know this path is aliased (courtesy of PathAuto) because I can see the path alias listed under URL Aliases as:
"bath-and-north-east-somerset"
So "taxonomy/term/45" is set to "bath-and-north-east-somerset" according to URL Aliases.
Is there a way to adjust my code so that when the jump menu jumps to the taxonomy listing the path reflects the proper taxonomy alias and not the generic "taxonomy/term/45"
Here is my code which is based on the code in this book page:
I got what I wanted by doing
I got what I wanted by doing the following:
I replaced:
With:
The str_replace function takes the " " (spaces) in the Taxonomy and swaps them for "-" (hyphens) which is what PathAuto uses to format them. If you do not do this, you will see "%20" in the URL everywhere there is a space in the Taxonomy.
Hope that helps someone.
Thanks again for the code snippet.
you can also accomplish this
you can also accomplish this by passing the relative url through the url() function:
Hope that helps!
6.x version List taxonomy drop down
Thanks, wongle.
I had a problem since upgrading from Drupal 5 to Drupal 6, where domain name was included and it got me an error where the tid got removed after using the drop down menu.
I also recommend using it only for registered members because all the keywords get listed on each page in html format and could impact your SEO position for each page.
Here is the code working on Drupal 6.x
Jump module
The jump module will do this for Drupal 5 and 6 -- see http://drupal.org/project/jump
-------
http://www.funnymonkey.com
Tools for Teachers
-------
http://www.funnymonkey.com
Same but do not show empty terms in drop down menu
For Drupal 5.x this will only show terms with nodes.
By the way using global redirect module will jump to the term alias.
Anyone knows how to disable
Anyone knows how to disable options in the list. For example, I would like to disable the root-level taxonomy nodes from selection while they should remain visible in the dropdown list. I need to add a option like "disabled"=>TRUE but not sure where in this form.
same problem
Hi,
I wonder if you ever figured this out. I'd like to do the same thing.
Any ideas on how to use two
Any ideas on how to use two of these in the same block can't seem to get it to work?
<?phpfunction
you could make your function a bit more generic to accept vid as a parameter, then call it multiple times (one for each vocabulary you wish to output)...
Hope that helps.
Adam
How do I add target and class attributes to this Jump Menu code?
Hi folks,
I have a form generated by this code.
It basically jumps to the URL created by the $tid as they are all extracted from the taxonomy_get_tree function.
I am trying to add two attributes to the URLs it jumps to:
1) target="_blank"
2) class=levelx (where x is the $tid to the URL)
I can't seem to inject it into the select box created by the forms API.
What am I missing?
Anyone have experience with this?
Thanks kindly.
'#attributes' =>
If you modify the attributes array, this should work (see first array member).
If you aren't using the JS bits, you can take out all of the onChange code etc, too....
You may need to mess with quotes around the class...
Hope that helps!
Adam
Answer my question
Is this you could take a look at my question on the forum. Thank you in advance!
subscribing
subscribing
How do select the previous
How can I default the previously selected option in the drop down? It is currently showing the first item. Some how I need to add "Selected" attribute to the last selected option. I tried several way, but couldn't make it work. Any help?