Hi there,
I'm trying to create a simple search block using a specific taxonomy vocabulary. As far as I can see things are working fine but I'm having issues getting the selected item back into a submit function. I added a submit button to the same form as the HS and hook_form_submit function. The only info I'm getting in $form_values at that function seems to be this:

Array ( [tid] => 0 [op] => go [submit] => go [form_token] => 96d0a78571c644397e5bb8c2c81f35a0 [form_id] => handset_search_dropdown_form )
Is there anything that I need to set up in my form to make this work? The code for the form itself looks like this:

function handset_search_dropdown_form() {
	$vid=7;
    $formname="Handset Make";
    
    $options[] = t('Handsets');
  
    $vocabulary = taxonomy_get_vocabulary($vid);
    
    if ($vocabulary->hierarchy > 0) {
    	$set = "searchy";
    	
    	$form[$set] = array(
    		'#type' => 'fieldset',
    		'#title' => t(''),
    	);
    	
    	$form[$set]['tid'] = array(
    		'#type' => 'hierarchical_select',
		    '#title' => t('Search handsets'),
		    '#options' => $options,
		    '#hierarchical_select_settings' => array(
	  			'module' => 'taxonomy',
      			'params' => array(
        			'vid' => $vid,
				),
    		),
    		'#default_value' => '0',
  		);
  		$form[$set]['submit'] = array(
  			'#type' => 'submit',
  			'#value' => t('go'),
  		);
    }
	 
	return $form;
}

Any help would be greatly appreciated.

CommentFileSizeAuthor
#4 handset_search.txt1.39 KBchrisivens

Comments

wim leers’s picture

Component: Code » Miscellaneous

$form_values['tid'] would be the one. You've submitted it with the default selection ("Handsets"), if you select something else, the corresponding term id (tid) will be there.

Unless you set $form[$set]['#tree'] = TRUE, then it'd be $form_values[$set]['tid'].

chrisivens’s picture

Thanks for the quick reply. I've tried a few things along the lines of what you have suggested but no matter what I select, I still get the tid =0 in the results. Do I need to populate the $options var with the full list to start off with or is this ignored when the javascript takes over? I've tried a couple of browsers but nothing seems to work.

wim leers’s picture

Please add a print_r($form);exit; just before your return $form; and post the output here.

chrisivens’s picture

StatusFileSize
new1.39 KB

Here's the output of the $form:

Array
(
    [searchy] => Array
        (
            [#type] => fieldset
            [#title] => 
            [tid] => Array
                (
                    [#type] => hierarchical_select
                    [#title] => Search handsets
                    [#options] => Array
                        (
                            [0] => Handsets
                        )

                    [#tree] => 1
                    [#hierarchical_select_settings] => Array
                        (
                            [module] => taxonomy
                            [params] => Array
                                (
                                    [vid] => 7
                                )
                        )
                )

            [submit] => Array
                (
                    [#type] => submit
                    [#value] => go
                )
        )
)

I have attached the module file as txt just in case it helps. Thanks for this. I think the #tree parameter is in a different place to the one you suggested but I was moving it around to try and get it to work. Am I right in thinking that the original form posted on the page is set to display:none and then a select created by the javascript in put in its place. When I looked into the source using the developer tools in firefox this seemed to be the case.

wim leers’s picture

Hah, you're going to want to hit yourself with a bat I think.

The problem is that you have only one option in $options. With key 0. Which is why you always get zero as the tid. Just add more options with keys >0, and it'll work ;)

chrisivens’s picture

That's the one thing that was stumping me. I was wondering whether the hierarchical select overrode the selection options or whether I needed to specify the full list of possible selections as per a normal select box. Writing this has made me think that I would have to do this anyway for it to degrade gracefully for non javascript clients... bah. It's the one thing I didn't try. I did try putting a couple of extra (bogus) entries in there to see if I could get it to select them but I guess they don't tie in with the 'real' data. One last thing, is there a quick and easy way to get a vocabulary without having to do a db_query? I suspect there is. If I get this thing working (tomorrow, as I'm shattered now) I'll post it in the snippets area or release as a small module as an example (all be it, a quick and dirty one).

After this little debacle, I wonder if it's possible to update the API document to go into a bit more detail as to how to use your module without using the views. Or just a more in-depth doc in general. It's a great idea and nicely generic. The demo page gives you a real taster of what is possible so but I think it could do with some source code to accompany it. Forgive me if you don't actually have anything to do with that site.

One again, thanks for your help with this issue. I love the drupal community. Everyone is so enthusiastic and helpful. Once I finish these few projects I'll be releasing a few small modules myself as a thank-you.

wim leers’s picture

Status: Active » Fixed

Hierarchical Select just creates a "layer" "on top of" the existing select. It still uses the original select, it just hides it. This makes graceful degradation super easy to support.

For questions like "is there a function that does foo?", I refer you to http://api.drupal.org/ and #drupal. When you're new, people will answer your questions gently. But if you repeat a question over and over again and get no answer, then you should know it's a dumb question, or at least a very googleable question.
The function you want is taxonomy_get_tree().

Here's a snippet to turn a vocabulary's terms into an $options array for use with a select: http://snipplr.com/view/4056/vocabulary-to-options/.

That site is my own site, yes. And - no offense - any somewhat experienced Drupal developer would have found this obvious, I think. Unless this question pops up again, I won't add it to the docs.

I'm glad you love Drupal as much as many of us do! :) And what you say there about contributing back, is exactly why this works: everybody helps in a way he/she can! :) Happy drupalling!

chrisivens’s picture

It's all fixed now, I had to get this thing fixed before going to bed and I did. I think the point at which I really stumbled was that Hierarchical Select just creates a "layer" "on top of" the existing select. I seemed to get it in my head that it took over completely. I guess I got stuck in that rut. I read up on the API docs and used taxonomy_get_tree and formed my array from there.

Thanks for your help, it's much appreciated. I've been writing modules and working with drupal for some time but never really used any jquery stuff or messed with taxonomy. I've just finished writing a single-sign-on module for drupal & sugarcrm using SOAP etc. but that's another story.

cheers

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.