0

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.

1

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.

0

Here's how I would do it in D7:

  1. Use hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id)
  2. Locate the #options list, it's in $form['tid']['#options']
  3. Loop through each term, use this method to check which terms are actually used
  4. Unset those unwanted terms.
  5. Done :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.