我知道它已經很長時間了,但仍然會爲了其他人尋找解決方案而分享。您將必須根據需要實現您自己的每種自動完成功能。需要注意的是使用前一個過濾器的值設置的autocomplete_path。休息應該是自我解釋。
// hook_menu would look something like this
function hook_menu() {
$menu = array();
$menu['hierarchical_filter'] = array(
'title' => 'Autocomplete Ajax Cascading Form',
'page callback' => 'drupal_get_form',
'page arguments' => array('hierarchical_filter_form'),
'access callback' => TRUE,
);
$menu['country_list'] = array(
'title' => 'Country List autocomplete',
'page callback' => 'country_list_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$menu['state_list'] = array(
'title' => 'State List autocomplete',
'page callback' => 'state_list_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$menu['city_list'] = array(
'title' => 'City List autocomplete',
'page callback' => 'city_list_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $menu;
}
function country_list_autocomplete($string='') {
$values = array('United States' => 'United States', 'South Africa' => 'South Africa', 'Russian Federation' => 'Russian Federation', 'Singapore' => 'Singapore', 'China' => 'China');
$matches = array_filter($values, function($item) use($string) {
if(stripos($item, $string) !== FALSE) return TRUE;
return FALSE;
});
drupal_json_output($matches);
}
function state_list_autocomplete($country_code, $state='') {
$values = array('South Africa' => array('Mpumalanga' => 'Mpumalanga', 'Gauteng' => 'Gauteng', 'Limpopo' => 'Limpopo', 'Northern Cape' => 'Northern Cape'),
'United States' => array('Alabama'=>'Alabama', 'Arizona'=>'Arizona'));
$matches = array_filter($values[$country_code], function($item) use($state) {
if(stripos($item, $state) !== FALSE) return TRUE;
return FALSE;
});
drupal_json_output($matches);
}
function city_list_autocomplete($state, $city='') {
$values = array('northern cape' => array('Barkly West' => 'Barkly West', 'Campbell' => 'Campbell', 'Delportshoop' => 'Delportshoop'),
'Alabama' => array('Butler'=>'Butler', 'Calera'=>'Calera', 'Helena'=>'Helena'));
$matches = array_filter($values[$state], function($item) use($city) {
if(stripos($item, $city) !== FALSE) return TRUE;
return FALSE;
});
drupal_json_output($matches);
}
function hierarchical_filter_form($form, &$form_state) {
$form = array();
$form['country_list'] = array(
'#type' => 'textfield',
'#title' => 'Choose Country',
'#autocomplete_path' => 'country_list',
'#ajax' => array(
'callback' => 'country_callback',
'wrapper' => 'states_wrapper',
),
);
$form['state_list'] = array(
'#type' => 'textfield',
'#title' => 'Choose State',
'#prefix' => '<div id="states_wrapper">',
'#suffix' => '</div>',
);
if(isset($form_state['values']['country_list'])) {
$form['state_list']['#autocomplete_path'] = 'state_list/'.$form_state['values']['country_list'];
$form['state_list']['#ajax'] = array(
'callback' => 'state_callback',
'wrapper' => 'city_wrapper',
);
}
$form['city_list'] = array(
'#type' => 'textfield',
'#title' => 'Choose City',
'#prefix' => '<div id="city_wrapper">',
'#suffix' => '</div>',
);
if(isset($form_state['values']['state_list'])) {
$form['city_list']['#autocomplete_path'] = 'city_list/'.$form_state['values']['state_list'];
}
return $form;
}
function country_callback($form, &$form_state) {
$commands = array();
// On changing country, make sure state and city fields are reset
$form['state_list']['#value'] = '';
$form['city_list']['#value'] = '';
$commands[] = ajax_command_replace('#states_wrapper', drupal_render($form['state_list']));
$commands[] = ajax_command_replace('#city_wrapper', drupal_render($form['city_list']));
return array('#type' => 'ajax', '#commands' => $commands);
}
function state_callback($form, &$form_state) {
// On changing state, make sure city field is reset
$form['city_list']['#value'] = '';
return $form['city_list'];
}