Recently I was working on a project that had a view with an exposed filter for country (via location module) and the dropdown list by default showed the entire country list, which was very large. I decided to write a code snippet that would reduce the select options to show only the countries that have been assigned to the nodes.
In my sandbox I create a simple node type for “person” that used the location module to capture the user’s country. I then created a view to show the people’s names and countries, and added an exposed filter for country.
Clicking on the exposed filter showed all of the available countries, which was a very large list.
I added the following code to my module to alter the exposed filter form and reduce the country list. (Please note, an alternative approach would be to modify the views object, I wanted to show how this could be accomplished in hook_form_alter())
Now the view only shows countries that match the resulting nodes.