Hello guys. I'm really having a hard time in joining my table's fields to the node table's fields. any body who can show some complete code on how to use it? a simple one will do.

Comments

mradcliffe’s picture

I know this is for users not nodes, but...

I posted a quick little view for the sessions table a couple of days ago here. Another quick file to look at is og_rsvp.

just wrap it in a function and add the return $data.

Anonymous’s picture

Im sorry it should be hook_views_data, In the advance help it says that it is used in describing table to views so that views will be aware of my custom table. The problem is that I don't have a proper direction or even a clear guide on how to do it. tried it a couple of times but still my table doesn't show up on the views settings.

heres my code:

function progressreport_views_data(){

  $data['node']['table']['base'] = array(
    'field' => 'nid',
    'title' => t('Node'),
    'help' => t("Nodes are a Drupal site's primary content."),
  );
  
  $data['reports']['table']['join']['node'] = array(
    'left_field' => 'nid',
    'field' => 'nid',
  );
  
  $data['node']['nid'] = array(
    'title' => t('Nid'),
    'help' => t('The node ID of the node.'),
  );
  
  $data['reports']['report_date'] = array(
    'title' => t('Report Date'),
    'help' => t('The date of the report'),
  );
  
  return $data;

}

I've also saved the file in reports.views.inc under includes folder which is inside my modules directory.

mradcliffe’s picture

A couple of things:

You do have the new hook_views_api() function in your .module file?

You don't need the ['base'] array as this is to declare new base tables.

$data['node']['nid'] = array(
    'title' => t('Nid'),
    'help' => t('The node ID of the node.'),
  );

You don't want to use a field that's already used by node_views.inc (or whatever file it is). Do you want to make reports.nid available in the view? If so then you want to make it $data['reports']['nid']....

  $data['reports']['report_date'] = array(
    'title' => t('Report Date'),
    'help' => t('The date of the report'),
  );

Also both of these don't really do anything but declare a database field with a title, but they're not going to show up anywhere without a filter, field, sort, argument, or relationship array.

You probably only want to display the report_date for a particular node right?


/**
 * This would work for a table that looked like this
 * table reports ( nid integer unsigned primary key, report_date timestamp )
 */

function progressreport_views_data(){
 
  $data['reports']['table']['join']['node'] = array(
    'left_field' => 'nid',
    'field' => 'nid',
  );
  
  $data['reports']['report_date'] = array(
    'title' => t('Report Date'),
    'help' => t('The date of the report'),
    'field' => array(
        'handler' => 'views_handler_field_date', // this is for Unix Timestamps, not for Date ISOs, see Date or Event_Views for that
        'click sortable' => TRUE
     ),
     // if I wanted a views filter, sort, etc.. I would do the same as field above but with the appropriate handler
    'filter' => array(
        'handler' => 'views_handler_field_date',
    )
  );
 
  return $data;

}

Also, make sure Views caching is turned off while you're testing.