I'm looking for some help with the Views 2 API. Wonder if anyone could help? I'm getting a bit frustrated, because I should be pretty close!

I have a Todo List module that defines a content type for the list itself, and for items on the list.
I've implemented hook_views_data() to describe the tables, but having trouble with one specific field.

All my Todo Items have a "parent" field, which in the database is just the node ID of the Todo List they belong to.

I want this to be used to display the node's title though, rather than the actual ID.

I realise there is a handler called "views_handler_argument_node_nid.inc", but I'm not sure how to go about implementing this properly.

Here is my views code:

function list_views_data()
{
	$data = array();

	// ok, two tables... in the List group
	$data['list_task']['table']['group'] = t('List');
	$data['node_list']['table']['group'] = t('List');
	
	// describe joins
	// 'list_task' joins 'node' through 'node_list'
	$data['list_task']['table']['join']['node'] = array(

		'left_table' => 'node_list',
		'left_field' => 'nid',
		'field' => 'parent',
	);

	// describe 'node_list' join to 'node'
	$data['node_list']['table']['join']['node'] = array(
		'left_field' => 'nid',
		'field' => 'nid',
	);

	// FIELD: task parent
	$data['list_task']['parent'] = array(
		'title' => t('Task parent'),
		'help' => t('The list this task belongs to'),
		'field' => array(
			'handler' => 'views_handler_field',
		),
	);

	// FIELD: task title
	$data['list_task']['task'] = array(
		'title' => t('Task title'),
		'help' => t('the title of the task item'),
		'field' => array(
			'handler' => 'views_handler_field',
			'click sortable' => FALSE,
		),
	);

	// FIELD: task assignee
	$data['list_task']['assignee'] = array(
		'title' => t('Task assignee'),
		'help' => t('The user this task is assigned to'),
		'field' => array(
			'handler' => 'views_handler_field',
			'click sortable' => FALSE,
		),
	);

	// FIELD: task completed
	$data['list_task']['completed'] = array(
		'title' => t('Task completion'),
		'help' => t('The completion status of the task'),
		'field' => array(
			'handlers' => 'views_handler_field_date',
			'click sortable' => FALSE,
		),
	);

	return $data;
}

Thanks!

Comments

mradcliffe’s picture

One way to do so is to bring the parent node in as a relationship -- if you want to display other things about the parent node.

 $data['list_task']['parent'] = array(
        'title' => t('Task parent'),
        'help' => t('The list this task belongs to'),
        'field' => array(
            'handler' => 'views_handler_field',
        ),
        'relationship' => array(
            'handler' => 'views_handler_relationship',
            'base' => 'node',
            'relationship field' => 'nid',
            'label' => t('Parent Node'), 
        ),
    );

The views 2 api site is down right now so I'm not sure if there is an arbitrary node lookup field handler (it would be pretty simple to make though although adding an extra query).

petebarnett’s picture

Thanks so much for your help.
That definitely beats writing my own handler to do the work! :)