How would I go about creating a drop down containing certain values from a datatable field when exposing a filter in the views module? wwhhhh that was a mouthful!

Comments

jvdurme’s picture

Mmm, just what I'm looking for.
Subscribe.

jvdurme’s picture

I got this working using the hook hook_form_FORM_ID_alter():

<?php
/**
* hook_form_FORM_ID_alter
*
*/
function MODULENAME_form_views_exposed_form_alter(&$form, $form_state) {
  $form['field_name']['#type'] = 'select';
  $form['field_name']['#multiple'] = false;
  $form['field_name']['#default_value'] = array("" => t("All"));
  $form['field_name']['#options'] = array("" => t("All"), "valueoption1" => t("Option 1"), "valueoption2" => t("Option 2"));
  $form['field_name']['#attributes'] = array('size'=>1);
}
?>

The form id in my case is "views_exposed_form" and I found this by calling the Devel module function:

dsm(get_defined_vars());

in a view theme template file and then looking for variables > view > exposed_data > form_id

Hope this helps anyone.

quinns’s picture

Hi,

Thanks to jvdurme I have a select-list with options from a data table and it works well. However, I cannot figure out how to enable the "All" option. I keep getting the "An illegal choice has been detected" error.

When I had the exposed filter as the default (text field), I also could not figure out how to get an "all results" option. If I left the field empty I got no results at all.

Anyone have an idea of how to get the "all results" working?

Thanks!

quinns’s picture

Ok, I have figured out how to allow "Any" searches.

In my case, I had an array of form elements called $carrier_name.

The trick was to use have the #default_value element as follows:

$form['carrier_name']['#default_value'] = $form['carrier_name']['#options'][''];

And, I set the first select option to have the label "Any" and no value to get the following markup:

<option selected="selected" value="">Any</option>

I hope this helps someone.