Where can I learn about how to create a custom exposed filter for Views 3 and D7? I am trying to learn how to create a custom exposed filter for a contributed module (select or other).  I found this tutorial for Drupal 6 but the code doesn't appear to work out-of-the-box on Drupal 7.

I also tried looking at the code in the hierarchical select module but it seems to be much more complex than what I am trying to do.

Does anyone have any suggestions for tutorials or modules that implement custom exposed filters in a relatively simple way (e.g., not a huge number of custom handlers like the location module) that I can learn from?
I was lurking around the internet trying to find answer for the same question and this is what I've got in result:


Implement several hooks in your custom module. Replace modulename and filtername with your actual names.

/**
 * Implements hook_views_api().
 */
function modulename_views_api() {
  return array(
    'api' =&gt; 2,
    'path' =&gt; drupal_get_path('module', 'modulename') . '/inc',
  );
}

/**
 * Implementation of hook_views_handlers() to register all of the basic handlers
 * views uses.
 */
function modulename_views_handlers() {
  return array(
    'info' =&gt; array(
      // path to handler files
      'path' =&gt; drupal_get_path('module', 'modulename') . '/inc',
    ),
    'handlers' =&gt; array(
      // register our custom filter, with the class/file name and parent class
      'modulename_handler_filter_filtername' =&gt; array(
        'parent' =&gt; 'views_handler_filter',
      ),
    ),
  );
}

function modulename_views_data() {
  $data = array();

  $data['node']['filtername'] = array(
    'group' =&gt; t('Custom'),
    'real field' =&gt; 'my_custom_filter_field',
    'title' =&gt; t('My custom filter'),
    'help' =&gt; t('Some more detailed description if you need it.'),
    'filter' =&gt; array(
      'handler' =&gt; 'modulename_handler_filter_filtername',
    ),
  );

  return $data;
}

Create folder named inc inside your module folder and create file named modulename_handler_filter_filtername.inc there (see code above for implicit reference to this file). Don't forget to use actual module and filter names.
Paste following code into that modulename_handler_filter_filtername.inc file. The code I used for this example creates a set of radio buttons that present years. So you can filter nodes by year created, using only years in which nodes were created.

class modulename_handler_filter_filtername extends views_handler_filter {

  /**
   * Options form subform for setting exposed filter options.
   */
  function value_form(&amp;$form, &amp;$form_state) {
    parent::value_form($form, $form_state);

    // get list of years from database
    $query = db_select('node', 'n');
    $query-&gt;addExpression("FROM_UNIXTIME(n.created, '%Y')", 'year');
    if (isset($this-&gt;view-&gt;filter['type'])) {
      $query-&gt;condition('n.type', $this-&gt;view-&gt;filter['type']-&gt;value, 'IN');
    }
    $result = $query-&gt;orderBy('year', 'ASC')
      -&gt;execute()
      -&gt;fetchAllAssoc('year');

    $years = array(
      '0' =&gt; t('All'),
    );
    foreach ($result as $k =&gt; $v) {
      $years[$k] = $k;
    }

    // create form element with options retrieved from database
    $form['value']['year'] = array(
      '#type' =&gt; 'radios',
      '#options' =&gt; $years,
      '#default_value' =&gt; end($years),
    );
  }

  /**
   * Alters Views query when filter is used.
   */
  function query() {
    // make sure base table is included in the query
    $this-&gt;ensure_my_table();

    // retrieve real filter name from view options
    // this requires 'real field' filter option to be set (see code above)
    $real_field_name = $this-&gt;real_field;
    // get the value of the submitted filter
    $value = $this-&gt;view-&gt;exposed_data[$real_field_name];

    // finally, alter Views query
    if (is_numeric($value) &amp;&amp; $value != 0) {
      /* 
        Having several custom exposed filters, make sure subsitution patterns
        (e.g. :filtername_value below) don't match across different filters.
        I spent some time figuring out why all my filters had the same value.
        It looks like the query skeleton is built first and then all replacements
        are made in bulk. Prefixing value with filter name looks good imo.
      */
      $this-&gt;query-&gt;add_where_expression($this-&gt;options['group'],
        "FROM_UNIXTIME(node.created, '%Y') = :filtername_value",
        array(':filtername_value' =&gt; $value));
    }
  }
}



That's all you need to get simplest custom exposed filter working!

Note that using FROM_UNIXTIME in condition of SELECT query may slow your database.