[support] Filter views on multiple fields

Pablo L. de Miranda pablolmiranda at gmail.com
Tue Sep 21 14:05:21 UTC 2010


Hi,

Below is a example how you can use the views api to make what you want:

<code>
/**
* Definning the form
*/
function filter_form(){
  // Get the filter information of your view
  $view = views_get_view(COGEAE_SAD_ALUNO_VIEW);
  $view->set_display(COGEAE_SAD_ALUNO_VIEW_DISPLAY_LISTA);
  $filters = &$view->display_handler->options['filters'];
  $options = array();
  foreach($filters as $filter){
	$options[$filter->id] = $filter->field;
  }
  $form = array();
  $form['filter_type'] = array(
	'#title' => t('Filter'),
	'#type' => 'select',
	'#options' => array('title' => t('Nome')),
	'#default_value' => 'nome'
  );
  $form['filter_query'] = array(
	'#title' => t('Text'),
	'#type' => 'textfield',
  );
  $form['submit'] = array(
	'#title' => t('Search'),
	'#type' => 'submit',
	'#value' => 'Filtrar'
  );

  return $form;

}
</code>

In this case, COGEAE_SAD_ALUNO_VIEW and
COGEAE_SAD_ALUNO_VIEW_DISPLAY_LISTA are the names of the view and the
display.
On submit method you have to do the same as the search module, and do
a redirect on your data:

<code>
/**
* Receiving the form information
*/
function filter_form_submit($form, &$form_state){
  $values = $form_state['values'];
  $filter_type = $values['filter_type'];
  $filter_query = $values['filter_query'];
  $form_state['redirect'] =
"phonebook/search/results/{$filter_type}/{$filter_query}";
}
</code>

Now you can filter the information using the parameters and render the view:

<code>
/**
* Using the views filter dinamically to filter you content
*/
function search_phonebook_results($filter_type = 'title', $filter_value = ''){
  $view = views_get_view(COGEAE_SAD_ALUNO_VIEW);
  $view->set_display(COGEAE_SAD_ALUNO_VIEW_DISPLAY_LISTA);
  $filters = &$view->display_handler->options['filters'];
  $filters[$filter_type]['value'] = $filter_value;
  $view->pre_execute();
  $view_content = $view->display_handler->preview();
  $view->post_execute();
  return $view_content;
}
</code>

I hope this help.

Att,

Pablo Lacerda de Miranda
Analista de Sistemas
Pontifícia Universidade Católica de São Paulo
pablolmiranda at gmail.com
+55 11 8701-1086


More information about the support mailing list