I am trying to build a custom exposed filter for a field using the Countries module.

Instead of listing all 200+ countries in the world, I want to list only three options Japan, Not Japan, and All (the site obviously has a Japan focus).

The sample code in this question is:

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();


The countries module stores countries as two-character ISO codes. Japan is JP. So I tried doing this:

    // Replace $this->value_options with an appropriately filtered list.

    $options = array();

    $options = array('jp' => t('Japan'));
    $options = array('us' => t('USA'));

    $this->value_options = $options;

However, this code results in an error. Additionally, I don't know how to specify the operator, and when I think about it, I'm not sure how to handle the operator conceptually, either.

The logic should be this: either JP or NOT-JP. However, as I understand it, Views does not provide per-item operators; the operator is applied to the whole handler. "Is one of..." "Is not one of..." etc.

So, to do Japan or Not Japan, should I list JP for Japan and then all other possible values for all other countries for Not Japan? (That wouldn't take too long, I'm just scared of missing one.) Or is there some better way?

I'd appreciate any insight into how Views operators work and what approach I should take to building such a list.

  • "However, this code results in an error." - could you quote that error? Your code looks good.
    – Mołot
    Oct 18, 2013 at 8:46
  • The error is this: Notice: Undefined index: jg in accept_exposed_input() (line 1260 of /sites/all/modules/contrib/views/handlers/views_handler_filter.inc). => I was confused because there is no 'jg' index defined, as far as I'm aware. Oct 18, 2013 at 16:05
  • 1
    If you are scared of missing one. Just get all possible options from where they are storred and loop through them. Also the function MY_MODULE_views_query_alter(&$view, &$query) might be something for you.
    – DD dev
    Oct 21, 2013 at 13:13
  • 1
    Side note: 'jp' is not the same as 'JP'
    – rooby
    Oct 21, 2013 at 13:18

2 Answers 2


This doesn't answer your question exactly but gives an alternative.

Views exposed filters have a grouping functionality that can be used to create predefined value/operator pairs, so you can configure it to give options like Japan & not Japan without actually giving the user the ability to select the operator.

Much nicer for the user.

See the attached screenshot as an example of a possible solution to your problem that requires no custom code (note I am guessing that JP is the correct value in this case).

enter image description here

There are a couple of things to be aware of:

  1. There is a bug where if you use is empty or is not empty you still have to enter a value, even though it is never used. See https://drupal.org/node/1818176
  2. You might get undesirable results if not all your content has country data. The reason is that if a field doesn't have data it is NULL and in views/sql you need 2 separate operators to match not a string vs not null. The grouping in views doesnt' let you select multiple operators so you cant have both "is not Japan" OR "is null". If your country data is a required field you have no problem here. - It's possible in this case you could alter the views query if "Not Japan" is selected and add in the "OR country IS NULL". You should be able to do that in hook_views_query_alter() and it still should be simpler than your current approach.

Note: I opened an issue for Add new filter operator "Is not equal to OR is empty"

  • Great alternative, this does exactly what I need without any custom code. Also, I really appreciate that you noted the bug, which is a huge potential gotcha. Oct 21, 2013 at 16:10

One more alternative is Views Filter Populate module also have the grouping functionality. See the Video Tutorial for better understanding. I think this module will handle your situation see the screenshot or you will get some idea with this or at least it will help the future visitors, since the question is about the operators.

Single exposed filter that combines search across multiple fields. In Views 3 and Drupal 6 or 7 it is as simple as installing the Views Filter Populate

enter image description here

Your Answer

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

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