Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I added an exposed filter (as described here) to let visitors filter the View by country.
But in the exposed filter select list, all enabled countries are listed. Is it possible to only list those countries that are already entered in the country field on nodes?
Comments
Comment #1
IanNorton CreditAttribution: IanNorton commentedI've got exactly the same requirement, I've started working on the solution, but it's a little rough around the edges and I'm not 100% sure there isn't a better way to do it using the views UI, I would expect that this is a requirement that comes up fairly often with views and lists.
Initially I worked to check if there are any nodes available for each country as the filter populated and didn't write the country name if so this seemed pretty query intensive, so I changed tact and came up with this below in template.php:
You'll note I've added caching to the code here as well and specified a content type to refine the query by, you may not want these but both were worthy additions in our case.
Comment #2
vinmassaro CreditAttribution: vinmassaro commented@IanNorton: I'm going to test your solution out - was this your final code? Thanks.
Comment #3
Alan D. CreditAttribution: Alan D. commentedSomething along these lines is the best bet. You may want a timeout on the cache, otherwise this data could get stale.
Closing to clean up the queue a bit.
Comment #4
IanNorton CreditAttribution: IanNorton commented@vinmassaro yes that was it, it's been working nicely since then.
Comment #6
RAWDESK CreditAttribution: RAWDESK commented#1 worked for me.
I even replaced the db_query with a more generic
$view = views_get_view();
$view->execute_display();