Views Filter Handler Node List

Specs

Version
Drupal 7
Tools
Database Abstraction Layer
Views Integrations
Created
02 Nov 2015

Summary

The Views module does not ship with a filter handler to filter a list of nodes of a given content type. This basic handler specifies a content type in its options and provides individual nodes as filter options. This can be used as a Views Exposed Filter.

After adding this to your module, a "Node List" filter will be available for node views.

Code

1. Define the Views API Version

MODULE_NAME.module
This is required when using the Views API in any module. It tells Views which API your module is using.
          
/*
 * Implements hook_views_api()
 */
function MODULE_NAME_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'MODULE_NAME') . '/views',
  );
}
          
          

3. hook_views_data() Implementation

MODULE_NAME.views.inc
MODULE_PATH/views
Define your Views handler using hook_views_data(). The handler specified here (views_handler_filter_node_list) needs to be the class name used in the next section.
          
/*
 * Implements hook_views_data()
 */
function MODULE_NAME_views_data() {

  $data['node']['node_list'] = array(
    'title' => t('Node List'),
    'real field' => 'nid',
    'help' => t('Provide a specific node type as filter options.'),
    'filter' => array(
      'handler' => 'views_handler_filter_node_list',
    ),
  );
 
  return $data;
}
          
          

4. Views Filter Handler Class

views_handler_filter_node_list.inc
MODULE_PATH/views/handlers
This contains the logic for the actual Views filter handler.
          
class views_handler_filter_node_list extends views_handler_filter_in_operator {
  
  function option_definition() {
    $options = parent::option_definition();
    $options['type'] = array('default' => 'article');
    return $options;
  }
  
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    
    $types = node_type_get_types();
    $options = array();
    foreach ($types as $type => $info) {
      $options[$type] = t($info->name);
    }
    
    $form['type'] = array(
      '#type' => 'select',
      '#title' => t('Content Type'),
      '#default_value' => $this->options['type'],
      '#options' => $options,
      '#fieldset' => 'alter',
    );
    
  }

  function construct() {
    parent::construct();
    $this->additional_fields['nid'] = 'nid';
  }
  
  function get_value_options() {
    if (!isset($this->value_options)) {
      $this->value_title = t('Content');
      $options = array();
      $results = db_query("SELECT * FROM {node} WHERE type = :type AND status = 1 ORDER BY title ASC", array(':type' => $this->options['type']));
      foreach ($results as $result) {
        $options[$result->nid] = t($result->title);
      }
      $this->value_options = $options;
    }
  }
}
          
          

Comments

Placeholder: I'll extend node comments with ajax and other functionality here.