Adding additional fields in Views
stanlew - August 19, 2007 - 17:01
Hi. I would like to add a field which displays a node's status using Views. What I did was:
Added this in function node_views_tables():
'status' => array(
'name' => t('Node: Status'),
'sortable' => false,
'notafield' => true,
'handler' => 'views_handler_node_status',
'help' => t('Display the status of a node.'),
),And added a handler:
function views_handler_node_status($fieldinfo, $fielddata, $value, $data) {
return $data->status ? t('Approved') : t('Not approved');
}But no matter what it always returns 'Not approved'. Did I leave out anything? Your help is very much appreciated.
Stanley

views_node.inc
I forgot to mention, the file I edited above is views_node.inc in modules/views/modules.
...
Your question isn't clear. Are you asking, in general, how to add a field, an arbitrary field, to a node?
Displaying a field, via Views, is only half of the story. The field has to exist: it has to be stored somewhere, to be managed by somebody.
==
BTW, there's indeed a field called 'status', in the node table. Is this a coinsidence that you chose the same name for your field? Note that the one in the node table doesn't stand for 'approved' but for 'published'.
...
Hi. I want to display the node's status (Published/Not published). I am trying to pull it off the node table, the values for it are 0 for Not published and 1 for Published (I prefer Approved/Not approved) - so added a handler in views_node.inc:
function views_handler_node_status($fieldinfo, $fielddata, $value, $data) {return $data->status ? t('Approved') : t('Not approved');
}
But it always returns 0. What else do I need to do? Thanks in advance.
...
The field isn't there beacuse nowhere you instruct Views to pull this field.
When your field is a pseudo one (
'notafield' => true) then Views doesn't know by itself what to load. You have to tell him explicitly what column(s) to load. The simplest method is to use the 'addlfields' array:'approved' => array(..
'addlfields' => array('status'),
),
(Another way to instruct Views to load some column(s) is by implementing a query handler.)
Use $value
I know this is an old post, but just for completeness...
$value contains the actual value returned from the record, so:
'status' => array(
'name' => t('Node: Status'),
'sortable' => false,
'handler' => 'views_handler_node_status',
'help' => t('Display the status of a node.'),
),
And the handler...
function views_handler_node_status($fieldinfo, $fielddata, $value, $data) {
return $value ? t('Yes') : t('No');
}