Hello,
I have created a views with a display page and exposed filters in block.
On my home page, I have placed the filters via the block.
In this block, I want populate the value of a filter from the value of another filter (a taxonomy structure), and use an ajax behavior for this.
In a hook_form_alter function, I overrides the code of filters. But, the ajax function don't work, the form seems not to be reload / rebuild when I select an option in my first filter. Then, the second filter isn't modified.
I have tested several options / codes without success.
Here is my code:
if ($form['#id'] == 'views-exposed-form-myview-page') {
// Create the options of the first filter
$form['field_myfieldA_tid']['#options'] = _get_associative_array_from_view(
'views_exposed_filter_tax', // view id
'default', // view display id
'tid', // key field id
'taxonomy_term_data_name', // value field id
'All'
);
$form['field_myfieldA_tid']['#ajax'] = array(
'callback' => '_update_myfieldB_callback',
'wrapper' => 'myfieldB_wrapper',
);
// Get the current option in myfieldA
$selected_myfieldA_id = $form_state['input']['field_myfieldA_tid'];
// Define the options of the second filter
if ($selected_myfieldA_id == 'All') {
// Options if no option in first filter
$myfieldB_options = array(
'All' => t("- Choose an option in first filter -"),
);
$form['field_myfieldB_tid']['#options'] = $myfieldB_options;
} else {
// Options if an option is selected in first filter
$form['field_myfieldB_tid']['#options'] = _get_associative_array_from_view(
'views_exposed_filter_tax_2', // view id
'default', // view display id
'tid', // key field id
'taxonomy_term_data_name', // value field id
$selected_myfieldA_id
);
}
$form['field_myfieldB_tid']['#prefix'] = '<div id="myfieldB_wrapper">';
$form['field_myfieldB_tid']['#suffix'] = '</div>';
//dpm($form_state['values']); // always empty
//dpm($form_state['input']); // always default value of each filter
}
..................................
function _update_myfieldB_callback($form, $form_state) {
return $form['field_myfieldB_tid'];
}
If I select an option in the first filter, I see on this field the ajax arrow, but the second field isn't modified. The form / page aren't reload/ rebuild.
Can you help me to find the problem? Do I must use and force a specific code to rebuild the form and the filters? With devel and dpm($form_state['input']), I get always the default value of each filter ("All").
Thanks for your help.