Working with operators when manually specifying options for a custom views exposed filter 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-&gt;value_options with an appropriately filtered list.
    $this-&gt;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-&gt;value_options with an appropriately filtered list.

    $options = array();

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

    $this-&gt;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.
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 &amp; 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).



There are a couple of things to be aware of:


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
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"