Add new comment

Hi, Everything is working great !
I got lost with views and terms Ids the last time.

I went a little further with 3 exposed views lists :
lista_paises with contextual filter « tipo »
lista_tipo with contextual filter « pais »
lista_temas with contextual filter « pais »

So I have
lista_paises → (actualizes) lista_tipo
lista_paises → lista_temas
lista_tipo → lista_pais

I added another ajax fonction I found here http://drupal.stackexchange.com/questions/8529/is-it-possible-to-replace...

Here's is my code which is a mess. This is not my job, I just copy and paste, try a bit and stop when it works.

<?php

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

  if(!empty(
$form_state['values'])) {
   
$form_state['input'] = array_merge($form_state['input'], $form_state['values']);
  } 
 
 
$form['tema']['#prefix'] = '<div id="temas_wrapper">';
 
$form['tema']['#suffix'] = '</div>';
 
$form['pais']['#prefix'] = '<div id="paises_wrapper">';
 
$form['pais']['#suffix'] = '</div>';
 
$form['tipo']['#prefix'] = '<div id="tipos_wrapper">';
 
$form['tipo']['#suffix'] = '</div>';

  if (
$form['#id'] == 'views-exposed-form-proyectos-busqueda') {
   
$selectedTema = $form_state['input']['tema'];
   
$selectedPais = $form_state['input']['pais'];
   
$selectedTipo = $form_state['input']['tipo'];

   
$form['pais']['#options'] = _get_associative_array_from_view(
     
'intel_projects', // view id
     
'lista_paises', // view display id
     
'taxonomy_term_data_node_tid', // key field id
     
'taxonomy_term_data_node_name', // value field id
     
$selectedTipo // term id of the selected Tipo
   
);

   
$form['tema']['#options'] = _get_associative_array_from_view(
     
'intel_projects', // view id
     
'lista_temas', // view display id
     
'taxonomy_term_data_node_tid', // key field id
     
'taxonomy_term_data_node_name', // value field id
     
$selectedPais // term id of the selected Pais
   
);
   
$form['tipo']['#options'] = _get_associative_array_from_view(
     
'intel_projects', // view id
     
'lista_tipos', // view display id
     
'taxonomy_term_data_node_tid', // key field id
     
'taxonomy_term_data_node_name', // value field id
     
$selectedPais // term id of the selected Pais
   
);
  }

 
$form['pais']['#ajax'] = array(
   
'callback' => 'ajax_exposed_fields_update_callback',
  );

 
$form['tipo']['#ajax'] = array(
   
'callback' => 'ajax_exposed_fields_update_callback',
  );

}

?>


<?php

function ajax_exposed_fields_update_callback(&$form, $form_state) {
  return array(
   
'#type' => 'ajax',
   
'#commands' => array(
     
ajax_command_replace("#tipos_wrapper", render($form['tipo'])),
     
ajax_command_replace("#temas_wrapper", render($form['tema'])),
     
ajax_command_replace("#paises_wrapper", render($form['pais']))
    )
  );
}

?>


<?php

function _get_associative_array_from_view($viewID, $viewDisplayID, $keyFieldID, $valueFieldID, $contextualFilter) {
 
 
$associativeArray = array();
 
$associativeArray['All'] = t('- Any -');

 
$viewResults = views_get_view_result($viewID, $viewDisplayID, $contextualFilter);

  foreach(
$viewResults as $viewRow) {
   
$associativeArray[$viewRow->$keyFieldID] = $viewRow->$valueFieldID;
  }

  return
$associativeArray;
}

?>

You can see it working here :
http://www.natate.org/proyectos-de-voluntariado

Is it possible to have the list lista_temas actualizaing the other selects ? Or are we stuck because contextual filter is limited to one term?