Remove or Filter Options on Exposed Filter in Drupal 7

2.1k views Asked by At

I'm working on a website that uses Domain Access and for a view I expose a filter that lists all domains. I want to list only some domains but not all. I know that it is posible to do with the filter configuration, but if I limit the options this way there is a problem with ajax which changes all the options text to "1", and in any case I want to know how to do it, with either hook form alter or with themes, but preferably using some hook from a module.

In general I'm trying to figure out how to remove some of the options for an exposed filter, I managed to do it using JQuery, but I'd like to do it with php, thanks!

1

There are 1 answers

2
Clive On

It should be fairly straight forward using hook_form_FORM_ID_alter as you suggest. First inspect the <form> element of the filter form (using Firebug or similar) and get it's ID attribute. The form's ID in Drupal will be that string but with all '-' characters replaced with '_' (so 'form-exposed-filter-form' would become 'form_exposed_filter_form').

The thing to remember is that the submit handler for the form will probably be expecting the elements you're trying to remove to be there, so you'll probably get unexpected results if you just yank them out of there.

The way round it is to change the type of the elements to value instead (so they're available to the submit function in $form_state['values']), and choose a default value for each. Something like this:

// Replace 'FILTER_FORM_ID' with the form's ID
function mymodule_form_FILTER_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $form['some_existing_element'] = array(
    '#type' => 'value',
    '#value' => $default_value_for_filter
  );
}