In the exposed filter form of a view, I have a taxonomy term selector set as a drop-down (you know, an HTML <select>
). By default, that dropdown includes all of the taxonomy terms in the vocabulary. Is there a way to limit the terms shown to only those that will return results (i.e. only show the terms that have been used)? Basically, I want to ensure that any of the available terms will return at least one result.
An easier approach without really coding a lot would be to create your search with Search API and/or Facets.
Facets are designed to expose only options that will return results.
https://www.drupal.org/project/facetapi
While you would have to rebuild the View, it probably is not a huge deal - and you won't have to write a bunch of code every time you expose taxonomy or similar fields as facets.
Here's how I would do it in D7:
- Use hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id)
- Locate the #options list, it's in $form['tid']['#options']
- Loop through each term, use this method to check which terms are actually used
- Unset those unwanted terms.
- Done :)