I use a custom module to assign nodes to my users. Each user gets assigned only one node and its a different node for each user. To do that, the module is using a database table to tell drupal, which node belongs to which user. The table consists of two fields: nid and uid. (In my usage scenario userreference or content profile weren't an option, so don't wonder why I am doing it this way.)

Now, I want to create a user view which contains the username, some of the user's profile fields and some CCK fields from the node that has been assigned to this user.

Of course, I have to describe the module's database table to views and define the relationships between the database tables involved. Unfortunately, even after hours of reading views API documentation I don't really get how to do this.

Here is what I have so far:

assign_node.views.inc

/**
 * Implementation of hook_views_data().
 */
function assign_node_views_data() {
  $data['assign_node']['table']['group'] = t('Assign nodes to users');

  $data['assign_node']['table']['join'] = array(
    'users' => array(
      'left_field' => 'uid',
      'field' => 'uid',
    ),
  );
  $data['assign_node']['uid'] = array(
    'title' => t('User ID'),
    'help' => t('The user ID of the user'),
    'relationship' => array(
      'base' => 'users',
      'base field' => 'uid',
      'handler' => 'views_handler_relationship',
    ),
  );
  $data['assign_node']['nid'] = array(
    'title' => t('Node ID'),
    'help' => t('The node ID of the node'),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'handler' => 'views_handler_relationship',
    ),
  );  
  return $data;
}

I know, it's neither perfect nor complete; it's just a starting point, but I don't know how to go on with this.
Anyone who can help?

Comments

extect’s picture

Status: Active » Fixed

Actually, I was much closer to the solution than I thought. Embarrassingly, I just forgot to put the following in my assign_node.module file:

function assign_node_views_api() {
  return array('api' => 2.0);
}

Just in case someone is trying to realise something similar, here is the final content of my assign_node.views.inc file:

/**
* Implementation of hook_views_data().
*/
function assign_node_views_data() {
  $data['assign_node']['table']['group'] = t('Assign nodes to users');

  $data['assign_node']['table']['join'] = array(
    'users' => array(
      'left_field' => 'uid',
      'field' => 'uid',
    ),
    'node' => array(
      'left_field' => 'nid',
      'field' => 'nid',
    ),
  );
  $data['assign_node']['uid'] = array(
    'title' => t('User ID'),
    'help' => t('The user ID of the user'),
    'relationship' => array(
      'base' => 'users',
      'base field' => 'uid',
      'handler' => 'views_handler_relationship',
      'skip base' => 'users',
    ),
  );
  $data['assign_node']['nid'] = array(
    'title' => t('Node ID'),
    'help' => t('The node ID of the node'),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'handler' => 'views_handler_relationship',
      'skip base' => 'node',
    ),
  ); 
  return $data;
}

I don't know if the above is best practice, but at least its working! ;-)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nzcodarnoc’s picture

I see that this thread is closed - but if the OP sees this I want to say "thank you, this was just what I was looking for".

wastrilith2k’s picture

I second that. This is a great example that is VERY helpful!

mcmacerson’s picture

If anyone is searching endlessly like I was thanks to BAD EXAMPLES of how to create a Views relationship, maybe you'll be helped by this little bit of advice. You probably need this line: 'base field' => 'nid', // The name of the field on the joined table. More information here: https://api.drupal.org/api/views/views.api.php/function/hook_views_data/7