I'm new to Drupal 7, is there anyone knows how can i create dependent hierarchical select list for country, city, region as a field?

Comments

rmeltg’s picture

Any thoughts...

I'm wondering the same thing right now.

I have a nested taxonomy and would like to create some dependant select boxes based on the taxonomy tree... is this possible?

arrakis83’s picture

You can try the Hierarchical Select (http://drupal.org/project/hierarchical_select).
Should solve your nested taxonomy issue.
There is also an demo page for nested taxonomy selection : http://wimleers.com/demo/hierarchical-select/taxonomy

graysadler’s picture

I'm very interested in this as well. Based off what I've researched, I don't think this capability is in core and no contrib modules support this either.

Lead Developer and Founder of StreamRiot.com

eddin’s picture

Subscribe

jurgenr’s picture

I'm working on this right now.

First of all I made a taxonomy tree with the hierarchy.

Then I made a module that makes a page with a form as described in the ajax examples module from http://drupal.org/project/examples
With some brainstorming even an novice like me could set the select boxes up into 3 levels with proper ajax calls.

example form elements:

	//Building the actual form elements
    $form['dropdown_first'] = array(
        '#type' => 'select',
        '#title' => 'Boat:',
        '#options' => $options_first,
        '#default_value' => isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : '',
        // Bind an ajax callback to the change event (which is the default for the
        // select form type) of the first dropdown. It will replace the second
        // dropdown when rebuilt
        '#ajax' => array(
            // When 'event' occurs, Drupal will perform an ajax request in the
            // background. Usually the default value is sufficient (eg. change for
            // select elements), but valid values include any jQuery event,
            // most notably 'mousedown', 'blur', and 'submit'.
            'event' => 'change',
            'callback' => 'taxterms_ajax_callback',
            'wrapper' => 'dropdown_second_replace',
        ),
    );
    
    $form['dropdown_second'] = array(
        '#type' => 'select',
        '#title' => 'Type:',
        '#prefix' => '<div id="dropdown_second_replace">',
        '#suffix' => '</div>',
    	'#options' => $options_second,
        '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
    	'#ajax' => array(
            'event' => 'change',
            'callback' => 'taxterms_ajax_callback2',
            'wrapper' => 'dropdown_third_replace',
        ),
    );

It's important to have an '#ajax' array with event and different! callback in every $form['element']

Then I used the function taxonomy_get_nested_tree as described in http://drupal.org/node/831398 (thnx to sylvaticus!) to put the taxonomy data inside my form select boxes.

example first dropdown:

// Get the list of options to populate the first dropdown.    						
    $nested = taxonomy_get_nested_tree(6, $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0);
    $options_first = array();
    	$options_first['select'] = 'Select';
  		foreach ($nested as $key => $value) {
    		$options_first[$value->tid] = $value->name;
   		}

Then for the following dropdowns use isset($form_state[...][...]) as seen in $value_dropdown_first in the example form module instead of $parent = 0 to declare the parent and get the right values as seen here:

// Get the list to populate the second dropdown.
	$tree = taxonomy_get_nested_tree(6, 0, isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first), 1);  
	$options_second = array();
		$options_second['select'] = 'Select';
  		foreach ($tree as $key => $value) {
    		$options_second[$value->tid] = $value->name;
       	}

Hopefully this can put you on the way and if not I'm allmost finished with my module so if anybody needs it I can set it online as an example.

Good luck,

Jurgen.

Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog

Futé’s picture

Hi, I am very interested too !

InverseFalcon’s picture

I would like an example of this, as I'm quite new to Drupal. If you've finished a module, a link to it would be wonderful. Otherwise, I have a few questions.

For example, I'm not sure what parts of your code are keywords or established code, and which are actually referring to your own content, nodes, or vocabulary. I'm also not certain if this is the whole picture, as you're referencing other snippets.

If you don't mind a short breakdown, that would be very helpful.

And if not, could just point out which elements refer to your own content? I've got a hierarchical vocabulary defined as well, and I'd just like to know in simple steps how to apply your solution.

Thanks!

jurgenr’s picture

You have to take a look at the examples module, there you can find the base of the code as shown above.
http://drupal.org/project/examples

I didn't finish the module because I found out that I could do allmost the same thing through views and the use of ajax wasn't really primary.

Good luck!

Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog

nicodv’s picture

Hello there, does anyone have a code example where it shows how to populate both dropdowns with data from db tables? I'm trying everything with my code, but I'm not good enough yet and it doesnt work.

Any help will be welcome.

THE VERY LITTLE AGENCY

nicodv’s picture

...finished your form? If you could post it, it would help me a lot. And by the way: have you populated the dropdowns from a db?

Any help is welcome

Thanks

THE VERY LITTLE AGENCY

geoandri’s picture

Hi

refer to this post (http://drupal.org/node/1358324) for data population from database.
If you could help me solve my problem also...

Sajjad Zaheer’s picture

This module is made for the same problem you have identified, i.e making dependent dropdowns for country state and city.

https://www.drupal.org/project/ajax_chain_select