How do you add custom module fields to the view "fields list"?

atrek - May 9, 2008 - 18:15

Hi,
I'm creating a custom module (overriding a node) and adding some extra fields stored in a custom table. I would like to make the (custom) fields available to the views module so we can create extra data in custom views. This page talks about the default fields available, but not how to add custom ones http://drupal.org/node/54448.

I've seen that the CCK adds fields to this list, but haven't been able to sort out how it's done.

Is there a table the fields are stored in that allow them to be picked up by the view module and populated into the fields list?

Any help would be appreciated.

Thanks!

Cheers,
Andy

Views API

Afief - May 9, 2008 - 18:21

You will have to get your module to interface with Views(and thus making it depend on it), You'd best check out the Views API for that
http://drupal.org/handbook/modules/views/api

Note: Use CCK if you can, there is a reason most drupal sites use both CCK and Views

Thanks, but found another challenge...

atrek - May 12, 2008 - 22:54

I'm not sure why it (the views api) didn't come up in my search, but there ya go.

I've got it working pretty well apart from some query handler problems. The query gets formed incorrectly so when it hits the db I get an error. The table joins are not in the correct order.

Here's the SQL that is formed by the Views api:

SELECT count( DISTINCT(node.nid))
FROM node node
INNER JOIN users users ON node.uid = users.uid
LEFT JOIN users users2 ON pstracker_projects.proj_lead = users2.uid
LEFT JOIN pstracker_projects pstracker_projects ON node.nid = pstracker_projects.nid
INNER JOIN node_access na ON na.nid = node.nid
WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all')
OR (na.gid = 2 AND na.realm = 'workflow_access')
OR (na.gid = 4 AND na.realm = 'workflow_access')
OR (na.gid = 2 AND na.realm = 'workflow_access_owner')))
AND ( (node.type IN ('pstracker')) )

and here's what it should be:

SELECT count( DISTINCT(node.nid))
FROM node node
INNER JOIN users users ON node.uid = users.uid
LEFT JOIN pstracker_projects pstracker_projects ON node.nid = pstracker_projects.nid
LEFT JOIN users users2 ON pstracker_projects.proj_lead = users2.uid
INNER JOIN node_access na ON na.nid = node.nid
WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all')
OR (na.gid = 2 AND na.realm = 'workflow_access')
OR (na.gid = 4 AND na.realm = 'workflow_access')
OR (na.gid = 2 AND na.realm = 'workflow_access_owner')))
AND ( (node.type IN ('pstracker')) )

The LEFT JOIN to pstracker_projects needs to be before the LEFT JOIN to users2.

Any ideas on how to control the order of the JOINS?

Thanks again!

Cheers,
Andy

 
 

Drupal is a registered trademark of Dries Buytaert.