How to make the exposed filter in the table wizard view a drop down menu or select list instead of simple textfield?

Comments

lina.irshaid’s picture

Priority: Normal » Critical
paadamson’s picture

Hi,

I created a module and used hook_form_views_exposed_form_alter()

You can then manipulate the form as you would any other:

$form['element']['#type'] = 'select';
$form['element']['#options'] = $my_array;

etc

achrelam’s picture

Hi Paul,

Am new to drupal, am trying to implement a new module to use hook_form_views_exposed_form_alter() as you did, but i cant find a good example.

I have the same issue, after the import from my table via tw, In the view generated, i can't use a drop down list or checkboxes instead of the normal text field.

I had a look at the filter handler, i also tried to use the php filter but i cant solve the problem.

Any help from you will be reaally appreciated
Thx,

paadamson’s picture

In your module (newmodule), create the function with the hook and inside the function make sure you're working on the correct form:

function newmodule_form_views_exposed_form_alter(&$form, $form_state) {
if (substr($form['#id'], 0, 25) == 'views-exposed-form-myform') {
// Function logic here
}
}

This will then let you work on the form via the form API like you would any other form within Drupal.

If you have a textfield called 'names' you can make this autocomplete by adding an autocomplete attribute:

$form['Names']['#autocomplete_path'] = 'path/to/name_autocomplete_callback';

Or replace the element with a select list:

$query = "SELECT name FROM {name_table} ORDER BY names";
$rs = db_query($query);
if ($rs) {
while ($data = db_fetch_object($rs)) {
$names[$data->name] = $data->name;
}
}
$form['Names']['#type'] = 'select';
$form['Names']['#multiple'] = false;
$form['Names']['#size'] = 1;
$form['Names']['#options'] = $names;

Here we do our own query on a table and pull it into an array ($names), then build a select element on the form and populate it with the array we just make.

There may be an easier way to make all this work, but I've found this manual method handy when all else fails.

brammm’s picture

Thank you so much!
I've made a select list using the db-query, works perfectly!
Much appreciated!