Drupal Technical
[Drupal] How to create a Hierarchical Select field for a Drupal 7 form using Form API?
Hierarchical select module helps us to display taxonomies, menu etc in a hierarchical form. It is easy to create a hierarchical form field as a content type's field. But have you tried implementing this in a custom drupal form?. Read on to know how to achieve the same.
Inside, your hook_form, Add the below code:
<?php
// Load includes/common.inc from the hierarchical select module.
module_load_include('inc', 'hierarchical_select', 'includes/common');
// Load category based in vocabulary machine name
$voc = 'Vocabulary machine name';//replace this with the taxonomy term
$vocabulary = taxonomy_vocabulary_machine_name_load($voc);
$form['Chapter_categories'] = array(
'#title' => t('Choose categories'),
'#type' => 'hierarchical_select',
'#config' => array(
'module' => 'hs_taxonomy',
'params' => array(
'vid' => (int) $vocabulary->vid,
'exclude_tid' => NULL,
'root_term' => NULL,
'entity_count_for_node_type' => NULL,
),
),
'#default_value' => $tids,
);
?>
If you add a drupal menu and call the above defined form, your form will have a field with name 'Chapter_categories' of type, 'heirarchical select'.Hope this helps!!.