I'd like to have a drop down box (not really a dropping menu) that pulls terms from a specific vocabulary. This drop down box needs to be displayed on all pages, within one fixed position.

It's essentially going to be right above all the content, in the middle of the page.

Can i accomplish this with FlexiNodes?

Im running Drupal 4.6, modified FriendsElectric theme (PHPTemplate driven).

I'm not that good with PHP code, so any snippets you can recommend are very much appreciated.

Thanks,
-=Vince

Comments

hadishon’s picture

I asked a similar question here: http://drupal.org/node/47247

I still haven't figured out how to do it yet. I'll let you know if I find out how to do it.

2 of my Drupal sites:
Small Farm Resource Center
Israel Travel Center

colorado’s picture

I'm populating a select menu (aka drop-down, jump menu) on my site by dynamically pulling terms from a particular vocabulary out of the taxonomy tables in the database. In addition I'm using the term description for the displayed select items. Here is the code I used:

//Default Value for Select
$t_description[] = 'List by State';
//Get Taxonomy Terms in Vocab State
$result = db_query("SELECT term_data.description, term_data.tid FROM term_data WHERE term_data.vid=1 ORDER BY description");
//Populate array with descriptions and links. The key of the array will be our URL.
while ($term = db_fetch_object($result)) {
  $t_description['http://www.americanpublicradio.com/gov/views/'.$term->description] = $term->description;
}
//Build the select list using Drupal's 'form_select' function.
$category_select = form_select('', 'category', '', $t_description, $description = NULL, 'on Change="top.location.href=document.getElementById(\'edit-category\').options[document.getElementById(\'edit-category\').selectedIndex].value"', $multiple = FALSE, $required = FALSE);
//Give our subsequent form a name we can grab with the Javascript
$form_attributes[description] = 'state_jump';
//Build our form with Drupal's 'form' function.
return form($category_select, $method = 'get', '', $form_attributes);

I based this code off of something I found on this site, I think in the handbooks, I just can't seem to find it again. There was also a different version with a submit button that didn't use any java script. If I can find it again I'll put up the link here. In the meantime, I hope this helps.

hadishon’s picture

Thanks Colorado!

This looks like something I'll be able to customize to fit some of my needs. I'll have to play around with it in the next couple days.

I think that's twice now that you've help me with something I needed. Thanks a lot!

2 of my Drupal sites:
Small Farm Resource Center
Israel Travel Center

colorado’s picture

I'm happy I can help! If I can at least answer more questions than I ask, then I think I bring value to the community. I encourage everyone to do the same :-)

(Hmm... I think I'll make that my new signature...)

I appreciate your kind words.

jasonwhat’s picture

This version might be a bit more useful as the other uses the description rather than the name and this example will take users to the taxonomy term page. It also adds a button for submit since I couldn't get the pure js way working on my site. Change .vid=10 to whatever vocab you want to select from. You can find the number by hovering over the edit vocab link on the admin taxonomy page.

<?php
//Default Value for Select
$t_description[] = 'List by State';
//Get Taxonomy Terms in Vocab State
$result = db_query("SELECT term_data.name, term_data.tid FROM term_data WHERE term_data.vid=10 ORDER BY name");
//Populate array with descriptions and links. The key of the array will be our URL.
while ($term = db_fetch_object($result)) {
  $t_description['http://mysite.com/taxonomy/term/'.$term->tid] = $term->name;
}
$category_select = form_select('', 'category', '', $t_description, $description = NULL, 'on Change="top.location.href=document.getElementById(\'edit-category\').options[document.getElementById(\'edit-category\').selectedIndex].value"', $multiple = FALSE, $required = FALSE);
//Add button javascript
$button_attributes[onClick] = 'top.location.href=document.getElementById(\'edit-category\').options[document.getElementById(\'edit-category\').selectedIndex].value';
//Add button
$submit_button = form_button('Go To Category!', $name = 'op', $type = 'button', $button_attributes);
//Create var for all form contents
$form_info = $category_select . $submit_button;
//Give our subsequent form a name we can grab with the Javascript
$form_attributes[name] = 'form';
//Build our form with Drupal's 'form' function.
return form($form_info, $method = 'get', '', $form_attributes);
?> 
serg-1’s picture

Hi,

I copied your code and changed the vid to reflcect the term I wanted "vid=1", I also changed mysite to the url representing my site. I pasted the new code into a block and got the following error message.

Fatal error: Call to undefined function: form_select() in /home/.laos/sergsoft/sergsoft.com/drupal7/includes/common.inc(1107) : eval()'d code on line 10

Line 10 of my code is:

$category_select = form_select('', 'category', '', $t_description, $description = NULL, 'on Change="top.location.href=document.getElementById(\'edit-category\').options[document.getElementById(\'edit-category\').selectedIndex].value"', $multiple = FALSE, $required = FALSE);

What do I need to change in order to get it to work. By the way, I'm testing this on Drupal 4.7 beta 4.

Any help would be greatly appreciated.

shanghaiguide’s picture

4.7 uses the new forms api.

Here is my working dropdown code.

VID = the taxonomy you want to select by.
FORMNAME = the name of the dropdown

<?php 

$vid=1;$formname="Country";

$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['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
 $print form_render($form[category]);

?>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=
http://www.shanghaiguide.com

pablov2’s picture

It dont appear, but if i change on the finished..

//Output the form
$print form_render($form[category]);


by

//Output the form
return form_render($form[category]);


The form appear, but dont work and give a warnning:

* warning: implode(): Bad arguments. in /var/www/vhosts/myhost/httpdocs/includes/form.inc on line 291.
* warning: implode(): Bad arguments. in /var/www/vhosts/myhost/httpdocs/includes/form.inc on line 291.

nigels’s picture

Hello, I don't know if anyone has worked out how to do this.
I have been trying with the code below which works, but at the same time keeps showing the error :-

warning: implode() [function.implode]: Bad arguments. in /var/www/html/craftscouncil/includes/form.inc on line 292.
warning: implode() [function.implode]: Bad arguments. in /var/www/html/craftscouncil/includes/form.inc on line 292.

If anyone has a fully working snippet for Taxonomy Jump Menu, or can tell me where I am going wrong with this I would be very greatful.

the code i'm using is printed below.

$vid=8;$formname="Country";
$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['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 form_render($form[category]);

marcoBauli’s picture

"return form_render" shows the dropdown, but also the two warnings above..

When i choose a country from the dropdown i am redirected to

mysite.com/<i>a_path_auto_term_here</i>/taxonomy/term/x.

Unfortunately that path term misleads my link and i get to a "page not found".

Any idea on how to solve this?

marcoBauli’s picture

using the full URL solves the problem with pathauto. Change:

  $options['taxonomy/term/'.$term->tid] = $term->name;

to

  $options['http://www.yoursite.com/taxonomy/term/'.$term->tid] = $term->name;

The two Warnings are still there though...

lekei’s picture

I hope to try this but I noticed the vid is hardcoded.

You need to set it to your vocabulary's vid or add code get the vid from the name.

marcoBauli’s picture

sorry lekei, somehow i missed your reply here..

yes, i changed the hardcoded $vid to reflect my category, but the warnings are still there.... :(

marcoBauli’s picture

we were using the wrong function.

Instead of form_render, drupal_get_form has to be used to kill thoose two Warnings.

Just change the very last line to:

return drupal_get_form('country_dropdown', $form);

and everything will run smooth! :)

chueewowee’s picture

In a block, I get the Parse error: syntax error, unexpected T_STRING in ../includes/common.inc(1158) : eval()'d code on line 28.

I cannot tell you why. I don't know.

chueewowee’s picture

Fatal error: Cannot use [] for reading in /nfsn/content/mightyzero/htdocs/includes/common.inc(1158) : eval()'d code on line 4

(drupal 4.7.2)

nsyll’s picture

drupal 4.7

$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 -->

 <select name="" onChange="top.location.href=document.getElementById( #039; #039;).options[document.getElementById( &#039; &#039;).selectedIndex].value" class="form-select" id="">

also i try
#attributes' => array('onChange' => "top.location.href=document.getElementById(\'$formname\').options[document.getElementById(\'$formname\').selectedIndex].value")
but nothing

hadishon’s picture

How can I have multiple drop down boxes that build a single link?

Basically:

http://mysite.com/taxonomy/term/1,2

or

http://mysite.com/taxonomy/term/1+2

So the visitor at the site selects a term from the first pull down menu and then selects another term for a differant vocab from a second pull down menu then hits submit to provide links like those above.

Let me know if I need to explain this more.

Michael
2 of my Drupal sites:
Small Farm Resource Center
Israel Travel Center

styro’s picture

http://drupal.org/node/55192 (the site is also listed in my sig) for an example of generating a 'filter' menu with terms from another vocab.

Late reply but I'm also part way into writing a 4.7 module to do the same thing a bit 'cleaner'. Instead of using a patched version of taxonomy_context it will be a standalone module that uses a block.

Let me know if you want any more info...

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

oziumjinx’s picture

how would I assign this drop down box to only pull terms from a specific vocabulary? Im planning on having two of these pull downs within my site, each for a specific vocabulary.

Im not too familiar with PHP or MySQL but if you can point out the locations and which values to alter, I would appreciate it.

Thanks,
-=Vince

jasonwhat’s picture

I put this above, you need to change the value of .vid=10 to be whatever number of your vocab. You find this by goint to the page admin/taxonomy and hovering over "edit vocabulary" and you will see the number in the url for that vocab.

StevenPatz’s picture

I modified this example to try to replicate the way TheOnion has their Archive Pulldown menu.

When I have:
$t_description['http://www.americanpublicradio.com/gov/views/'.$term->description] = $term->description; set to my specific path it works great in Firefox, but in IE it keeps concatenating the url to the end of whatever page I am on.

Any ideas?

colorado’s picture

Yes I had the very same problem when I just used the specific (relative) path URL. It may not be the best solution, but I use the absolute URL as is used in the code snippet you posted (i.e. http://mydrupalsite.com/views/etc...), NOT just a relative path URL (i.e. views/etc...). As long as you use an absolute URL path there I think it should work fine.

---
"Please drupal responsibly: give as much help as you get."

StevenPatz’s picture

That's odd but it works. Thanks.

coupet’s picture

good example, maybe contribute to documentation.

Apache is bandwidth limited, PHP is CPU limited, and MySQL is memory limited.

colorado’s picture

Here's that link I was looking for, that I used as a starting point for the example code above:

http://drupal.org/node/17762

---
To keep the community strong, please answer as many questions as you ask.

oziumjinx’s picture

Im still new to Drupal and need a bit more direction. Should i be placing this code within my page.tpl.php file in the correct location, or does this need to go into a block I create?

Also, Im wondering if this code can be embedded within

tags so that I can style the formatting of the drop down box, and the area that it sits within.

Thanks again for the help.

-=Vince

jasonwhat’s picture

You can place the code in a block, in a node or in your template depending where you want it. If you choose a block or node be sure that the php filter is selected. Yes, php lets you put tags within the code. Just use regular div tags and be sure to close them.

coupet’s picture

Great contribs!
what about submitting to PHP snippets ?

Apache is bandwidth limited, PHP is CPU limited, and MySQL is memory limited.

alext-1’s picture

Here is a little code snippet that might be of use to someone:

It allows hierarchies to show in multiple select fields....

  $form['category'] = _taxonomy_term_select(
  											"category to search", 
  											"category", 
  											$edit['category'], 
  											2, 
  											t('Select the name of the category you are searching for.'), 
  											TRUE, 
  											"", 
  											array()
  										);
  										
colorado’s picture

Thanks for the snippet, but where does it go, and which drupal version does it work with?

---
"Please drupal responsibly: give as much help as you get."

alext-1’s picture

I have created a special form for inputting businesses. each business is associated with a category which is defined with taxonomy.

here is a snippet of that function.

hope this helps.

function business_search() {
//this would be the 
     	
  $form['category'] = _taxonomy_term_select(
  											"category to search", 
  											"category", 
  											$edit['category'], 
  											2, //or whatever your category id associated with taxonomy is..
  											t('Select the name of the category you are searching for.'), 
  											False, 
  											"", 
  											array()
  										);
  										


	$form['name'] = array(
		'#type' => 'textfield',
		'#title' => t('name of business'),
		'#default_value' => $edit['name'],
		'#size' => 60,
		'#maxlength' => 64,
    '#autocomplete_path' => 'business/autocomplete/',		
		'#description' => t('Enter the name of the business you are searching for.'),
	);
	
//
//other form elements here



//finally the submit button

	$form['action'] = array(
		'#type' => 'submit',
		'#value' => t('Search'),
	);
  
  
  
  $output = drupal_get_form('business_search', $form);
  return $output;



}


jasonwhat’s picture

cool contribution. Do you have an example of this so I could see exactly what it does? Also, I see the word "category" a lot in there. Is this for the standard taxonomy or category module? Finally, does this work only with terms with subterms, or also with vocabs?

colorado’s picture

OK, here's a complete, 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.yoursite.com/taxonomy/term/'.$term->tid] = $term->name;, replace yoursite.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 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.yoursite.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);

Thanks to everyone who contributed to this thread!

---
"Please drupal responsibly: give as much help as you get."

manuj_78’s picture

Is it working on some site. would be interesting to see it working somewhere

marcoBauli’s picture

except for a couple of things:

1. the dropdown menu lists only the main categories in the vocabulary, but does not list children ones:

example:

Voc:
Countries
Cats:
Argentina
Armenia
Brazil
--Cearà
--Paranà

it lists only 'Argentina, Armenia, Brazil'

2. It odes not respect URL aliases: it always redirects to '/node/n', even if that node is aliased as '/country/brazil'

This apart, all the rest works fine as aspected, thank you!

colorado’s picture

I hope someone better at PHP than I am can add those capabilities. It's probably something simple.

---
"Please drupal responsibly: give as much help as you get."

cappelle’s picture

I'm using Drupal 5.0 RC1 and get the following error message:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'X_dropdown' was given in /usr/home/web/snl152654/includes/form.inc on line 217.

where X is the name of the vocabulary.

I'm inserting it using the Front Page module.

colorado’s picture

No, I have only taken it as far as 4.7.

HOWEVER, the Views Filter Block module produces similar results and DOES have a version for 5.x in development:

http://drupal.org/project/views_filterblock

---
"Please drupal responsibly: give as much help as you get."

cappelle’s picture

Thanks for the advice, I will definitely try it out as soon as I get the Views module to work. But this might require me to change hosts first...

I described the problem here:

http://drupal.org/node/108278)

svogel’s picture

There seems to be a solution in the snippet section for 5.0:

http://drupal.org/comment/reply/48843/186101

Best regards
Stefan

adminfor@inforo.com.ar’s picture

I posted this today in the snippet book page http://drupal.org/node/91924#comment-209160
Hope this helps
RGdS, Gustavo

adminfor@inforo.com.ar’s picture

I´ve implemented it creating nodes as explained by colorado in the comment above http://drupal.org/node/48843#comment-167478

The implementation was really easy, no more than an hour. I really want to thanks colorado.

Manuj, if you're still interested, take a look in
http://www.inforo.com.ar/AccesoRapido2

Regards,
Gustavo

colorado’s picture

So glad it helped someone!!

---
"Please drupal responsibly: give as much help as you get."

eTech’s picture

Anyone working on the URL alias problem?

Many thnx in advance.

colorado’s picture

There is now a module, Views Filter Block, that accomplishes this for Drupal versions 4.7.x and 5.x:

http://drupal.org/project/views_filterblock

---
"Please drupal responsibly: give as much help as you get."

hbhavsar007’s picture

Hi,

I installed quiz module.After that i created one Vocabulary using admin/cateroies addvocabulary with multichoice type after that I created some sub vocabulary under that now when I am going to create quiz and going to addquestion tab I can choose vocabulary which i created but when i press button callled "filter question list" the question are not filter as per vocabulary.

Do you have any solution regarding my query ??