Custom options in your views exposed filters

Posted by: 
Dominique De Cooman

We ll do this with an example as usual.

We have a table with book entries forming a top 100 which gets and update every week. Each entry has a current position, a last week position, a corresponding node id and a week.
We ll create a file in called book_top100.views.inc, in that file we ll implement hook_views_data() and a hook_views_handlers().

The hook_views_data() will expose our custom table to views and joining it to the node table.

<?php
/**
 * Implementation of hook_views_data()
 */
function boek_top100_views_data() {
  
$data['boek_top100']['table']['group'] = t('Top 100');
  
  
$data['boek_top100']['table']['base'] = array(
    
'field' => 'nid',
    
'title' => t('Top 100 nodes'),
    
'help' => t('Holds book Top 100'),
  );
  
$data['boek_top100']['table']['join']['node'] = array(
    
'left_table' => 'node',
    
'left_field' => 'nid',
    
'field' => 'nid'
  );
  
$data['boek_top100']['vorige_week'] = array (
    
'title' => t('Last week postion'),
    
'help' => t('Last week postion'),
    
'field' => array(
      
'handler' => 'views_handler_field_numeric',
    ),
    
'filter' => array(
      
'handler' => 'views_handler_filter_numeric',
    ),
    
'sort' => array(
      
'handler' => 'views_handler_sort',
    ),
  );
  
$data['boek_top100']['positie'] = array (
    
'title' => t('The current position'),
    
'help' => t('The current position'),
    
'field' => array(
      
'handler' => 'views_handler_field_numeric',
    ),
    
'filter' => array(
      
'handler' => 'views_handler_filter_numeric',
    ),
    
'sort' => array(
      
'handler' => 'views_handler_sort',
    ),
  );
  
$data['boek_top100']['week'] = array(
    
'title' => t('The current week'),
    
'help' => t('The current week'),
    
'field' => array(
      
'handler' => 'views_handler_field',
    ),
    
'filter' => array(
      
'handler' => 'boek_top100_views_handler_filter_many_to_one',
    ),
    
'sort' => array(
      
'handler' => 'views_handler_sort',
    ),
  );
    
  return 
$data;
  
}
?>

Still in the same file. The hook_views handlers will show views our custom handler. We extend the views_handler_filter_many_to_one filter by telling views where to call for data to put in the exposed filter. Our goal is to filter our nodes by a certain weeks top 100.

<?php
/**
 * Implementation of hook views_handlers()
 */
function boek_top100_views_handlers() {
  return array(
    
'info' => array(
      
'path' => drupal_get_path('module''boek_top100') . '/inc',
    ),
    
'handlers' => array(
      
'boek_top100_views_handler_filter_many_to_one' => array(
        
'parent' => 'views_handler_filter_many_to_one',
      ),      
    ),
  );
}
?>

In the next file located in /inc/boek_top100_views_handler_filter_many_to_one.inc we ll put the class override to get our custom data. The only thing we do is tell it which callback to call for custom data.

<?php
class boek_top100_views_handler_filter_many_to_one extends views_handler_filter_many_to_one {
  function 
get_value_options() {
    if (isset(
$this->value_options)) {
      return;
    }
    
    
$this->value_options boek_top100_get_week_options();       
  }
}
?>

In our module file we ll put which api version we ll be using and the callback boek_top100_get_week_options() we defined in our handler file to get our custom options.

<?php
/**
 * implementation of hook_views_api()
 */
function boek_top100_views_api() {
   return array(
    
'api' => 2.0,
  );
}

/**
 * Get the options for the top100 views exposed filter
 */
function boek_top100_get_week_options() {
  
$result db_query("SELECT DISTINCT week FROM {boek_top100}");
    
  while (
$row db_fetch_object($result)) {
    
$options[$row->week] = $row->week;
  }
  return 
$options;
}
?>

When we now create a view of type node and we order the nodes of type book we will be able to sort on current position and we ll be able to filter on which top100 of which week you want to see by adding and exposing the week filter in the views admin interface.

Add new comment