Answers.Body: <p>You might find this module even better than you expect:</p>
  
  <p>Views Selective Exposed Filters (views_filters_selective)</p>
  
  <p>"Restrict exposed filter values to those present in the result set."</p>
  
  <p>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.</p>
  
  <p><a href="https://www.drupal.org/project/views_selective_filters" rel="nofollow noreferrer">https://www.drupal.org/project/views_selective_filters</a></p>
  
  <p>[thanks Aidan]</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-07T08:27:25.467Z
Answers.Text: https://www.drupal.org/project/views_selective_filters
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-07T10:17:57.940Z
Answers.Text: THIS TOTALLY ROCKS !! :-)
Answers.UserDisplayName: commonpike
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-01T10:38:19.300Z
Answers.Text: 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.
Answers.UserDisplayName: commonpike
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-06T19:34:34.507Z
Answers.LastActivityDate: 2019-12-09T15:40:57.250Z
Answers.LastEditDate: 2019-12-09T15:40:57.250Z
Answers.UserDisplayName: Alan Dixon
Answers.Body: <blockquote>
    <p>Or any other solution someone found, like limiting the dropdown to used countries only ?</p>
  </blockquote>
  
  <p>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"</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-07T10:09:59.047Z
Answers.Text: 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.
Answers.UserDisplayName: commonpike
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-08T02:51:30.117Z
Answers.Text: right. i hadn't taken that nuance on board - looks like Alan has a great solution then
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-06T19:12:20.077Z
Answers.LastActivityDate: 2019-12-06T19:12:20.077Z
Answers.UserDisplayName: petednz - fuzion
Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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). </p>
  
  <p>Is there any way to pull the country table into the view and, for example, rewrite the view using <code>hooks_views_query_alter</code> to allow you to filter on the country name ? </p>
  
  <p>Or any other solution someone found, like limiting the dropdown to used countries only ?</p>
  
Tags: <search><drupal-views><addresses>
Title: Drupal views: text search in country name
ThreadText: # Drupal views: text search in country name
  
  (Asked by: username_0 on 2019-12-06)
  
  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 ?
  
  **Tags:** search, drupal-views, addresses
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-12-06. Score: 2 | Accepted answer
  
  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 username_2]
  
  > **Comments:**
  > **username_2**: https://www.drupal.org/project/views_selective_filters  
  > **username_0**: THIS TOTALLY ROCKS !! :-)  
  > **username_0**: 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.  
  
  ---
  
  ### Answer by username_3 on 2019-12-06. Score: 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"
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_3**: right. i hadn't taken that nuance on board - looks like Alan has a great solution then  
  
  ---
  
  