i'm trying to show the vote count for each node from my module - it's a reworking of the plus1 module from pdd1, where there's a table called plus1_vote, with fields, uid, nid, vote and createdtime. i want to retrieve count(*) for each nid .

function plus1_views_tables(){
	$tables['plus1_vote'] = 
		array(
			'name' => 'plus1_vote',
			'join' => array(
				'type' => 'inner',
				'left' => array ('table' => 'node', 'field' => 'nid'),
				'right' => array ('field' => 'nid'),
			),

			'fields' => array(
				'vote' => array(
					'name' => t('Plus1: Vote count'),
					'help' => t('Display by vote count'),
					'notafield' => true,	// not a field
					'sortable' => true,	// allow the user to sort by vote count
					'handler' => '_plus1_views_votes_handler',
					'query_handler' => '_plus1_views_votes_queryhandler',
					'field' => 'count(*)',
				)	,
			),
		);
	return $tables;
}

function _plus1_views_votes_queryhandler($field, $fieldinfo, &$query){
	$query->add_field($field['field'], $field['tablename'], $field['queryname']);
	$query->add_orderby('plus1_vote', 'vote', 'asc');
	$query->add_groupby('node.nid');
}

i don't want to retrieve the field vote (as that's always 1) but the count of votes. how do i do this?

ps. how about an 'api' component for views!

Comments

George2’s picture

i suppose the query i want to perform is similar to this:

select n.nid,title, count(*) from node n
inner join plus1_vote pv on pv.nid = n.nid
group by n.nid
order by count(*) desc

sun’s picture

Project: Views (for Drupal 7) » Voting API

Sorry, this issue cannot be solved in Views.

eaton’s picture

Status: Active » Closed (won't fix)

This is an original module, not one that uses VotingAPI. VotingAPI solves this by pre-calculating count and sum values.