I blew a whole lot of time today trying to get the hang of programming with Views. I'm stumped on a particular problem.

I'm working on Ubercart Auction. It adds a table called {uc_auction} which has an nid field and a high_bidder field. The high_bidder field contains the uid of the user who is the current high bidder for the node being auctioned. What I want to do is be able to display a link to the high bidder's profile along with the high bidder's name. However, I seem to be incorrectly joining {uc_auction} to {users}, because Views insists on joining {node}.uid to {users}.uid instead of {uc_auction}.high_bidder to {users}.uid as is desired.

Relevant parts of uc_auction.views.inc:

<?php
function uc_auction_views_data() {
	return array(
		'uc_auction' => array(		
			'high_bidder' => array(
				'title' => t('Current high bidder'),
				'help' => t('The current high bidder of the auction (or the winner if the auction has expired).'),
				'group' => t('Product'),
				'field' => array(
					'handler' => 'uc_auction_handler_field_high_bidder',
					'additional fields' => array(
						'name' => array(
							'table' => 'users',
							'field' => 'name',
						),
					),
					'click sortable' => TRUE,
				),
				'argument' => array(
					'handler' => 'views_handler_argument_string',
				),

				'sort' => array(
					'handler' => 'views_handler_sort',
				),
			),
			'table' => array(
				'group' => t('Node'),
				'join' => array(
					'users' => array(
						'left_field' => 'high_bidder',
						'field' => 'uid',
					),
				),
			),
		),
	);
}

/**
 * Implementation of hook_views_handlers() (a Views hook).
 */

function uc_auction_views_handlers() {
	return array(
		'info' => array(
			'path' => drupal_get_path('module', 'uc_auction') . '/views',
		),
		'handlers' => array(
			'uc_auction_handler_field_high_bidder' => array(
				'parent' => 'views_handler_field',
			),
		),
	);
}
?>

And views/uc_auction_handler_field_high_bidder.inc:

<?php
class uc_auction_handler_field_high_bidder extends views_handler_field {
	function render($values) {
		if ($values->uc_auction_high_bidder == 0) {
			return t('Anonymous');
		}
		elseif (user_access('access user profiles')) {
			return l($values->users_name, 'user/' . $values->uc_auction_high_bidder, array('html' => TRUE));
		}
		else {
			return $values->users_name;
		}
	}
}
?>

This code produces a link to the correct user's profile, but the name is that of the node author, not the high bidder.

Thanks in advance if you can help point me in the right direction.

Comments

Garrett Albright’s picture

Just to add: Queries produced by this code look like this:

SELECT [snip] uc_auction.high_bidder AS uc_auction_high_bidder, users.name AS users_name FROM node node [snip] INNER JOIN users users ON node.uid = users.uid
pndur’s picture

Maybe you can find what you are looking for in user_relationship_views.views.inc
Hope this will help.

pndur’s picture

I've adapted this file from the views module
views_handler_field_comment_username.inc

It gives you a link with the uid to the user profile, not the user name -> user profile.

Just copy the file to your views directory and modify it like this:

class uc_auction_handler_field_high_bidder extends views_handler_field {
and:
$this->additional_fields['high_bidder'] = 'high_bidder';
and the following line to:
$account->uid = $values->{$this->aliases['high_bidder']};

If you adapt it to display the username instead of the uid, then it will probably be what you're looking for; or get you started.

mooffie’s picture

You don't need to write any handlers. You only need to tell Views:
1. How to connect your table to {node}; and:
2. That the 'high_bidder' column points to a user.

Something along of:

$data['uc_auction']['table']['group']  = t('UC Auct');
$data['uc_auction']['table']['join'] = array(
  'node' => array(
    'left_field' => 'nid',
    'field' => 'nid',
  ),
);

$data['uc_auction']['high_bidder'] = array(
  'title' => t('User'),
  'help' => t('The current high bidder'),
  'relationship' => array(
    'handler' => 'views_handler_relationship',
    'base' => 'users',
    'field' => 'uid',
    'label' => t('bidder'),
  ),
);

return $data;

(Expect typos.)

mortenson’s picture

I dont know that module but what about saving the uid of the user who is the current high bidder in the node. This can be done with a hidden field.