2

When viewing CiviCRM contacts in a Drupal view, you can filter on the Address:Country , but the filter widget proposes you to filter on one or more of the 300+ countries in CiviCRM.

When exposing this filter, the user sees those 300+ countries. If only ~50 countries actually have contacts, such a dropdown is not very useful. Most countries will return 0 results.

It would be good enough if I can have a text search on the name of the country, but I can't get that to work. The filter only supplies the country id (eg using a 'combine' filter on the single field requires you to enter a number as a search string - that works).

Is there any way to pull the country table into the view and, for example, rewrite the view using hooks_views_query_alter to allow you to filter on the country name ?

Or any other solution someone found, like limiting the dropdown to used countries only ?

2 Answers 2

2

You might find this module even better than you expect:

Views Selective Exposed Filters (views_filters_selective)

"Restrict exposed filter values to those present in the result set."

Whenever you're looking at a selection (or subselection), it'll filter the select options to only those options that are in the result set. So you never have the issue of pointlessly offering filter options that will give the visitor an empty result.

https://www.drupal.org/project/views_selective_filters

[thanks Aidan]

3
  • 1
    drupal.org/project/views_selective_filters
    – Aidan
    Commented Dec 7, 2019 at 8:27
  • THIS TOTALLY ROCKS !! :-)
    – commonpike
    Commented Dec 7, 2019 at 10:17
  • This works for countries. It also works for State/Province ID. Oddly, it doesn't work for State/province - the 'selective' filter just does not appear, even though I have the field in the field list.
    – commonpike
    Commented May 1, 2023 at 10:38
1

Or any other solution someone found, like limiting the dropdown to used countries only ?

In a Views filter of Country you should be able to select the ~50 that you want listed, and then set the option that says something like "limit to selected items only"

2
  • Thank you - yes, except in this case, members can be from all over the world, so I don't know which countries I want listed - that should be a dynamic query.
    – commonpike
    Commented Dec 7, 2019 at 10:09
  • right. i hadn't taken that nuance on board - looks like Alan has a great solution then
    – petednz - fuzion
    Commented Dec 8, 2019 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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