在drupal 7中依赖多选下拉列表

时间:2015-01-09 12:04:42

标签: php drupal drupal-7

我是Drupal的新手,在我的项目中我有2个多选下拉框。我不知道如何使用ajax在drupal形式的alter function中执行依赖多选下拉动作。

单选依赖下拉选择正在运行。但是当我在2个多选择依赖下拉的情况下尝试相同时,我无法找到任何解决方案。在多选错误"检测到非法选择。请联系网站管理员。"显示。

请帮助我如何在drupal中使用2个多选依赖下拉。

这是我的代码:here" field_department"是第一个多选下拉列表。 " field_policy_categories"是第二个多选择下拉菜单。我想要第二个下拉值对应于第一个字段的多选选项。

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'];
}

0 个答案:

没有答案