Integration with Views - views.inc totally rewritten

mariano.barcia - July 13, 2007 - 20:32
Project:Node Images
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Description

This code goes into node_images.views.inc, and it's 100% re-written from scratch, because the current plugin is seriously broken.

HTH

<?php
// $Id: node_images.views.inc,v 1.3 2007/02/03 01:28:22 stefano73 Exp $

/**
* @file
* Views API hook implementations for the node_images module.
*/

function node_images_views_tables() {
 
$tables['node_images'] = array(
   
'name' => 'node_images',
   
'join' => array(
     
'left' => array(
       
'table' => 'node',
       
'field' => 'nid',
      ),
     
'right' => array(
       
'field' => 'nid'
     
),
    ),
   
'fields' => array(
     
'all_images' => array(
       
'name' => t('Node Images: Display node images'),
       
'notafield' => true,
       
'query_handler' => 'views_query_handler_node_images',
       
'handler' => array(
         
'views_handler_node_images' => t('All images'),
        ),
       
'option' => array(
         
'#type' => 'select',
         
'#options' => array(
           
'thumbs' => t('Thumbnails'),
           
'fullsize' => t('Full size images'),
          )),
       
'sortable' => false,
       
'help' => t('Display all node images in one field.'),
      ),
    ),
   
'filters' => array(
     
'nid' => array(
       
'name' => t('Node images: Has node images'),
       
'operator' => array('=' => t('Exists')),
       
'list' => 'views_handler_operator_yesno',
       
'list-type' => 'select',
       
'handler' => '_node_images_handler_images_exist',
       
'help' => t('Filter whether the node has node images.'),
      ),
    ),
    );
  return
$tables;
}

function
views_query_handler_node_images($field, $fieldinfo, &$query) {
 
$query->add_field('nid', 'node');
}

/**
* Handler for displaying all images of a node
*/
function views_handler_node_images($fieldinfo, $fielddata, $value, $data) {
  if (
$listed) {
   
$and = " AND list = 1";
  }

 
$node = node_load(array('nid' => $data->nid));
 
$result = db_query("SELECT ni.* FROM {node_images} ni INNER JOIN {node} n ON n.nid = ni.nid WHERE ni.nid = %d $and", $data->nid);
  while (
$object = db_fetch_object($result)) {
   
$node->node_images[$object->id] = $object;
  }
  return
theme('node_images_view', $node, TRUE, FALSE, $fielddata['options']);
}

/**
* Views handler for filtering
*/
function node_images_handler_images_exist($op, $filter, $filterdata, &$query) {
  switch (
$op) {
    case
'handler':
     
$query->ensure_table('node_images');
      if (
$filter['value']) {
       
$query->add_where('node_images.id');
      }
      else {
       
$query->add_where('ISNULL(node_images.id)');
      }
    break;
  }
}
?>

#1

mariano.barcia - August 30, 2007 - 01:01

Ups, there is a typo above, in the filter handler. The line

'handler' => '_node_images_handler_images_exist',

should be

'handler' => 'node_images_handler_images_exist',

(without the underscore prefix)
HTH

#2

marcoBauli - December 18, 2007 - 14:43

mariano: could you please explain what does "the current plugin is seriously broken" mean? What does not work? cheers

#3

mariano.barcia - January 17, 2008 - 10:16

marco,
From what I can recall, nothing worked, so it was useless.
HTH!

#4

greg.harvey - February 4, 2008 - 17:01
Version:4.7.x-2.x-dev» 5.x-1.x-dev

This worked fine for me. I added an extra field option called "lightest_image" to just show the image at the top of the available images too. Patch attached.

AttachmentSize
node_images.views_.inc_.patch 6.87 KB
 
 

Drupal is a registered trademark of Dries Buytaert.