Views Filter Handler Node List

Specs

Version
Drupal 7
Tools
Database Abstraction Layer
Views Integrations
Created
28 Feb 2016

Summary

El módulo de Vistas no se entrega con un controlador de filtro para filtrar una lista de nodos de un tipo de contenido determinado. Este manejador de base especifica un tipo de contenido en sus opciones y proporciona nodos individuales como opciones de filtro. Esto puede ser usado como un filtro expuesto Vistas.

Después de añadir esto a su módulo, un filtro de "lista de nodos" estará disponible para las vistas de nodo.

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.