1

In Drupal 6, I've created a custom View type that allows me to query data in my custom table. One of my fields, uid, relates to drupal users. In the Views I create for my custom type, I can display user info for each record by using a simple join in hook_views_data():

$data['users']['table']['join']['awesome_user_submissions'] = array(  
  'left_field' => 'uid',
  'field' => 'uid',
);

Now, I'd also like to display Profile fields in my view (eg: profile_first_name). Unfortunately, it doesn't appear that this will be a simple table join. How can I do this?


Solution: (thanks @googletorp)

Don't use the join I originally used (above). Instead replace it with the following in hook_views_data(), then I can use fields from the 'users' table, as well as from the 'profile_' tables.

$data['awesome_user_submissions']['uid'] = array(
    'title' => t('User'),
    'help' => t('Relate an awesome submission to the user who entered it.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'base field' => 'uid',
      'label' => t('user'),
    ),
  );

1 Answer 1

0

Views itself makes a join to the uid, so if you add the Users uid as a relationship, you should be able to get the profile fields as well.

Alternatively you could join the profile field table yourself.

For reference this is how Views makes the profile -> user join:

$data['profile_values']['table']['group']  = t('Profile');

$data['profile_values']['table']['join'] = array(
  // The node table
  'node' => array(
    'left_table' => 'profile_values',
    'left_field' => 'uid',
    'field' => 'uid',
  ),
  // The users table
  'users' => array(
    'left_table' => 'profile_values',
    'left_field' => 'uid',
    'field' => 'uid',
  ),
);
1
  • Eureka! Yes, I should have been enabling a relationship to "users" from my custom View type, instead of trying to join the 'users' table in hook_views_data().
    – rcourtna
    Jul 4, 2011 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.