Hi,

I'm writing a custom module and I've been stuck on trying to get 4 tables to join in views.
Here's the deal, I have Organic Groups and I want them to be associated with user roles. So I created a table with fields 'rid' and 'gid'. Now I need to create a view with argument the gid/nid of the OG. The view should show all the users associated with the user role associated with the OG.

I'm really stuck on how to do this. I think I need to get the users table and my custom table to join up. I tried using relationships, got stuck. Any help?

Comments

qde’s picture

I'm not an expert of Drupal and views.. ..but I already did something like that.
I think you have to :

  • Create a new file : your_module.views.inc
  • Implements the hook views_data
  • Set the links between tables.

Example :

I have a table called "news" that contains a nid (node id), vid and a date.
My ....views.inc file looks like :



function table_news_views_data()  {
  // Basic table information.
  // ----------------------------------------------------------------
  //  New group within Views called 'News'
  //  The group will appear in the UI in the dropdown tha allows you
  //  to narrow down which fields and filters are available.

  $data = array();
  $data['table_news']['table']['group']  = t('News');

  // Let Views know that our example table joins to the 'node'
  // base table. This means it will be available when listing
  // nodes and automatically make its fields appear.
  //
  // We also show up for node revisions.
  $data['table_news']['table']['join'] = array(                // <==== Define the link between node table and my table_news table.
    'node' => array(
      'field' => 'nid',
      'left_table' => 'table_news',
      'left_field' => 'nid',
    ),
  );

  // Date
  $data['table_news']['date'] = array(
    'title' => t('Date'),
    'help' => t('Date of news'),
    'field' => array(
      'handler' => 'views_handler_field_date',
      'click sortable' => TRUE,
     ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
  );



  return $data;
}

I hope it will help.

P.S :
Excuse me for my english. That's not my first language.

BetaTheta’s picture

Thanks, that did help. I figured out that in my specific case I needed this following bit of code

$data['chapters']['table']['join']['users'] = array(
    'left_table' => 'users_roles',
    'left_field' => 'rid',
    'field' => 'rid',
  );

chapters is the table containing the nid-rid information. By using left table, I was able to join the chapters table to the users table through the users_roles tables.