3

I'm new to Drupal , in my project I have 2 multiple select drop down box. I don't know how to do dependent multi select drop down action in drupal form alter function using ajax.

Single selection dependent drop down selection is working. But when i tried the same in case of 2 multi select dependent drop down i couldn't find any solution . On multi select error "An illegal choice has been detected. Please contact the site administrator." is shown.

Please help me how to use 2 multi select dependent drop down in drupal.

Here is my code : here "field_department" is the 1st multi select drop down. "field_policy_categories" is the 2nd muti select drop down. i want 2nd drop down values corresponding to 1st field's multi selected options.

function policy_node_form(){
$form['field_department'] = array(
    '#type' => 'select',
    '#title' => 'Department',
    '#multiple' => TRUE,
    '#options' => array(
        '' => t('-Select-'),
        '1444' => t('department1'),
        '1445' => t('department2'),
        '1446' => t('department3'),
        '1447' => t('department4'),
        '1448' => t('department5'),
        '1449' => t('department6')                      
        )       
);
$form['field_policy_categories'] = array(
    '#type' => 'select',
    '#title' => 'Categories',
     '#multiple' => TRUE,
    '#options' => array()
);

}
function page_form_alter(&$form, &$form_state, $form_id) {

if ($form_id == 'policy_node_form') {
$category = array();
$category[''] = '- None -'; 

$form['field_department']['und']['#ajax'] = array(         
    'wrapper' => 'policy_category_list',
    'callback' => 'ajax_policy_category_callback',
    'method' => 'replace',
);

$form['field_policy_categories']['#validated'] = TRUE;
$form['field_file_upload']['#validated'] = TRUE;

if (isset($form_state['values']['field_department'])) {            

    $array_departments = array($form_state['values']['field_department']['und']['0']['tid']);

    $query = db_select('field_data_field_department', 'f');
    $query->join('taxonomy_term_data', 't', 'f.entity_id = t.tid');
    $query->join('taxonomy_term_hierarchy', 'h', 't.tid = h.tid');

    $query->condition('f.entity_type', 'taxonomy_term', '=');
    $query->condition('f.bundle', 'policy_categories', '=');            
    $query->condition('f.field_department_tid', $array_departments, 'IN');
    $query->condition('h.parent', 0, '=');
    $query->fields('f');
    $query->fields('t');
    $query->fields('h');
    $result = $query->execute();

    $policy_vid = taxonomy_vocabulary_machine_name_load('policy_categories')->vid;

    $category[''] = '- None -';

    foreach ($result as $record) {
        $parent_arr = taxonomy_get_parents_all($record->tid);
        $parent_level = sizeof($parent_arr);
        $parent_level--;
        $parent_tid = $parent_arr[$parent_level]->tid;
        $parent_name = $parent_arr[$parent_level]->name;
        $category[$parent_tid] = $parent_name;

        $policy_children_arr = taxonomy_get_tree($policy_vid, $parent_tid);
        foreach ($policy_children_arr as $key => $policy_children) {
            $depth = '';
            for ($i = $policy_children->depth; $i >= 0; $i--)
                $depth .= '--';
            $category[$policy_children->tid] = $depth . $policy_children->name;
        }
    }

    $form['field_policy_categories']['und']['#options'] = $category;
} else {

    $form['field_policy_categories']['und']['#options'] = $category;
}

$form['field_policy_categories']['#prefix'] = '<div id="policy_category_list">';
$form['field_policy_categories']['#suffix'] = '</div>';

$form['#validate'][] = 'form_validate_policy_add';
}
}
function ajax_policy_category_callback($form, &$form_state) {

return $form['field_policy_categories'];
}
  • Have you gone through this article: drupal.org/node/1124052 It says they are having problem only with multi-option-select box and not normal select boxes. So your ajax using 2 normal select boxes should work. For debugging start with simple array values instead of query. After you confirm its working replace the array values with your query. – abhiklpm Jan 13 '15 at 19:02
  • You are telling us about AJAX problems, but do not share any AJAX code. If the code you pasted is in your AJAX callback, we can't even see it, because you omitted beginning of it, and didn't show how it is attached to form elements. – Mołot Jan 14 '15 at 8:16
  • Can you suggest any other method for dependent selection of 2 multiple selection drop down box in drupal 7. – Neethu PH Jan 15 '15 at 11:33
2

To try take your second $form['field_policy_categories']['und']['#options'] to individual function instead of inside hook_form_alter().

function page_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'policy_node_form') {
    $form['field_department']['und']['#ajax'] = array(         
      'wrapper' => 'policy_category_list',
      'callback' => 'ajax_policy_category_callback',
      'method' => 'replace',
  );

  $form['field_policy_categories']['#validated'] = TRUE;
  $form['field_file_upload']['#validated'] = TRUE;

  $form['field_policy_categories']['und']['#options'] = categories_options($form_state);

  $form['field_policy_categories']['#prefix'] = '<div id="policy_category_list">';
  $form['field_policy_categories']['#suffix'] = '</div>';

  $form['#validate'][] = 'form_validate_policy_add';
}

function categories_options($form_state) {
  // option go to here
}

.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.