5

I have an exposed filter "country" for my member list view (I'm using addressfield module to store the country). I would like to only show countries in my exposed filters where I have members. I can manually restrict the filter by select countries.

Which one would be the best option, between the following ones?

  • theming views-exposed-form.tpl.php
  • using hook_form_alter()
  • hiding some fields with CSS
1
  • Have you solved your problem? Would you be willing to share the solution with us?
    – deinqwertz
    Dec 8, 2012 at 17:38

4 Answers 4

11
+50

I just did something similar for some data I'm exposing in Views for my custom module (it's kinda the same process no matter where views is getting its data).

To do this in the most efficient/easy way possible, you should create your own filter handler.

For a really simple example:

<?php
class custom_handler_filter_countries extends views_handler_filter_string {
  /**
   * Shortcut to display the exposed options form.
   */
  function value_form(&$form, &$form_state) {
    $form['value'] = array(
      '#type' => 'select',
      '#title' => t('Countries'),
      '#options' => function_that_returns_options_you_want(),
    );

    return $form;
  }
}
?>

Then, in your module's .info file, add the file containing this class (in this example, 'custom_handler_filter_countries.inc') to the .info file's files array (files[] = includes/custom_handler_filter_countries.inc).

To make your particular view use this views handler instead of the default handler, you should be able to change the handler in hook_views_pre_view(), I think. (On my particular site, since I defined the views data table myself, I just set my custom handler for fields that needed it. For your site, since the normal handler is already defined by Address Field, you need to override the filter handler.

4

My team once attempted to do the same, in D6. Details are a bit hazy by now, but I'll share my experience and solution.

Initially, we went with a hook_form_alter() solution that removed all non-existant entries.

In our particular case however, we had several displays with exposed blocks, and as far as we could determine, views gave them the same form-id, making the solution "unstable". For this reason, we implemented the form ourselves. This may or may not be an issue for you, depending on Core/Views version, and number of displays.

We ended up implementing a new block, copied the form structure from Views so it would be recognized as an exposed form, and added the existing entries instead of removing the non-existent ones.

The solution works, but if I were to do it again, I would look at the Views Selective Exposed Filters module first, and see how it's done.
There is also an "official" attempt to solve this.

3

First of all, what do you mean by programmatically? I have a sense that you don't mean that you ABSOLUTELY NEED to write code in order to achieve your goal, but you basically want your view to "automatically" filter the exposed filters for you (based on which ones are being used).

What you actually need to do is get a count of the countries that have members. There was a module for that kind of thing in Drupal 6 (Term Node Count) which was specifically targeted to retrieve the number of nodes attached to a particular term.

The thing is, though, that you don't even need a module for that. With Views 3 you can use Relationships and the "Group By" option (Group Type: Count) to have your View determine on the fly if there are any nodes that are using this country.

If you absolutely need custom code for your solution, I would recommend taking a look at the attempt for D7 Port for Term Node Count or a very simple (rather general though) explanation of how to setup your Views with relationships to achieve your goal can be found in this comment from the same Term Node Count port discussion.

1
  • 1
    I wrote "programmatically" because I couldn't find a module that would do it and figured I have to write some code. I don't think the views3 "group by" feature has any effect on the exposed filter selection. Also, the addressfield module doesn't use Taxonomies for the country selection, therefore "Term Node Count" won't help much.
    – uwe
    Jan 2, 2012 at 2:52
1

The Views selective filters module will only show options that are present in the result set, perfect for this use case.

This module allows to have an exposed filter only show options that belong to result set.

1
  • Hi and welcome to Drupal Answers.
    – Wtower
    Feb 1, 2015 at 16:52

Your Answer

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

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