I have a form with a hierarchical select element and an activeselect element. I would like to be able to change the valid list of options on the activeselect element once the user has selected a value from the deepest level of the hierarchical select element.

For example :

  $form['item1'] = array(
    '#type' => 'hierarchical_select',
    '#title' => t('Item 1'),
    '#size' => 1,
    '#config' => array(
      'config_id' => 'taxonomy-views-viewname-26',
      'save_lineage' => 0,
      'enforce_deepest' => 0,
      'entity_count' => 0,
      'resizable' => 0,
      'level_labels' => array(
        'status' => 0,
        'labels' => array(
          '0' => '',
          '1' => '')),
      'dropbox' => array(
        'status' => 0,
        'limit' => 0,
        'reset_hs' => 1,
        'title' => ''),
      'editability' => array(
        'status' => 0,
        'item_types' => array(),
        'allowed_levels' => array(),
        'allow_new_levels' => 0,
        'max_levels' => 3),
      'module' => 'hs_taxonomy_views',
      'params' => array(
        'optional' => '',
        'vid' => 26,
        'exclude_tid' => '',
        'root_term' => ''),
      'exclusive_lineages' => array('0' => '**ALL**'),
      'render_flat_select' => 1,
    ),
  );

  $form['item2'] = array(
    '#tree' => 1,
    'nids' => array(
      '#type' => 'activeselect',
      '#title' => 'Item 2',
      '#multiple' => 0,
      '#size' => 0,
      '#options' => array(
         0 => 'All'
       ),
      '#required' => 0,
      '#description' => '',
      '#theme' => 'activeselect',
      '#activeselect_path' => 'activepath',
      '#activeselect_targets' => 'item3',
    ),
  );

Is this possible ? My JavaScript is not the best, but can I do this with the change-hierarchical-select event or is there a hierarchical select hook I can use ?

I tried using the ahah forms bindings, but they did not fire when attached the hierarchical select element above.

Thanks

Comments

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Active » Fixed

#ahah won't work, you'll have to write some custom JS. The change-hierarchical-select event is indeed the one you want.

drenton’s picture

Thanks Wim. This is what I did to get it working.

Added this JavaScript to my form :

  drupal_add_js(
    "$(document).ready(function(){
      $('#hierarchical-select-0-wrapper')
      .bind('change-hierarchical-select', function(hsid, updateType, settings) {
        var selectedTermTid = $('#' + settings.select_id).val();
        var frmDrupal = function(data) {
          var result = Drupal.parseJson(data);
          $('#edit-item2-nids').html(result['options']);
        }
        $.get('/our_hook_menu_path/' + selectedTermTid, null, frmDrupal);
        return false;
      });
    });",
    'inline'
  );

Added this to our module :

  // placed in hook_menu
    $items[] = array(
      'path'     => 'our_hook_menu_path',
      'callback' => 'our_callback',
      'type'     => MENU_CALLBACK
    );

function our_callback() {  

  $options = array();
  $options[0] = "";
  $options[1] = "option1";

  foreach ($options as $key => $value) {
    $output = $output . '<option value="' . $key . '">' . $value . '</option>';
  }
  
  print drupal_to_js(array('options' => $output) );

  exit();

}
wim leers’s picture

Cool, thanks for posting that! I'm sure it'll be useful for others :)

Status: Fixed » Closed (fixed)

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

Ashu’s picture

Thanks

it really helped... :)