1

On my site, users can input their nationality in a field provided by the Countries module. About half the users on my site are Japanese, and the other half come from all other countries.

I have a view that allows members to search for each other, and I want to make the nationality an exposed filter. Since there are 200+ countries in the world, I do not want to expose the entire list of countries (this would be a selection nightmare on mobile phones).

Instead, I would like to provide a select list with three options: All countries, Japan, and Not Japan.

The Views UI allows administrators to select options on select lists and limit the list to the selected options by checking a box. This is not what I want to do, because there is no way to create a Not Japan option (which should show all members from all countries except Japan).

This answer suggests writing a custom views filter handler, but I'm a bit confused about where to begin.

The Countries module provides its own custom handlers, but (I think) these are overkill for what I need (you have to add a relationship to the country, which allows you to filter by continent, etc.); I simply need to make a version of the basic select list handler with my three options.

It seems that most views handlers extend another. In the views/handlers folder there is a file, views_handler_filter_string.inc, which I think I can use, but I'm not really sure what the next step is to take.

1

I've done something very similar to that with the following process:

  1. Implement hook_views_api() in a custom module

    function MYMODULE_views_api() {
      return array(
        'api' => 3,
        'path' => drupal_get_path('module', 'MYMODULE') . '/includes/views',
      );
    }
    
  2. Create includes/views/views_handler_filter_MYMODULE_country.inc somewhere with code similar to this (the MYMODULE bit isn't necessary, just a bit of low-tech namespacing):

    class views_handler_filter_MYMODULE_country extends views_handler_filter_in_operator {
    
      /**
      * Get value options for the select list.
      */
      function get_value_options() {
        // Replace $this->value_options with an appropriately filtered list.
        $this->value_options = function_to_build_filtered_list();
      }
    
    }
    
  3. Register that file with the autoloader by adding this to MYMODULE.info:

    files[] = includes/views/views_handler_filter_MYMODULE_country.inc
    
  4. Finally, implement hook_field_views_data_alter() to force Views to use your new handler, changing the $field['field_name'] condition (and other similar strings based on field_address) to whatever makes sense for your site

    function MYMODULE_field_views_data_alter(&$result, $field, $module) {
      // Limit country exposed filters.
      if ($module == 'addressfield' && $field['field_name'] == 'field_address') {
        $result['field_data_field_address']['field_address_country']['filter']['handler'] = 'views_handler_filter_MYMODULE_country';
      }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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