Implicit Relationships
I'm just getting starting creating field, filter, etc handlers for Views for a project.
I've hit the point where I can't get further from existing examples and the API documentation.
I'm working with table, let's call it "Stuff"
Among the stored fields is a user ID.
I'd like to make it easy for someone creating a view of the table to show the username associated with that user ID.
I have absolutely no trouble creating a relationship handler that lets a user create a view that gives them access to all the fields in the user table. I suppose I could force the user to do that.
What I'd like instead is for the person creating a view from the "Stuff" table to see "Stuff: Username" as a choice for a field.
I suppose if I needed to I could write a field handler that loaded the user from the user ID and rendered a username. I'm not sure why, but this seems duplicative and not particularly good behavior for performance.
Is there a way, in the Views API framework, to accomplish what I want by describing the way the Stuff and Users tables are related and using the existing Views handlers for the username rendering?
In other words I'd like the relationship between "Stuff" and "users" to be implicit in my stuff_views module rather than explicit as a relationship in the Views interface.
I hoped I picked a good title.
Thanks

If I understand your
If I understand your question correctly you absolutely can. What you have to do tell views to join the user table to your table on the appropriate field.
Assuming your my_stuff table has a uid field you would do something like this in your implementation of hook_views_data():
<?php$data['users']['table']['join'] = array(
'my_stuff' => array(
'left_field' => 'uid',
'field' => 'uid',
'type' => 'INNER',
),
);
?>
This essentially pulls anything from the user group into your view so that your users can see them in the fields, filters, arguments etc. You should still define a relationship because they are a bit more flexible but I think this gives you what you want.
If you want to look at some examples of this check out the implementations in views/modules/user.views.inc and views/modules/node.views.inc.
Many many thanks. It works
Many many thanks.
It works of course.
I was using
$data['my_stuff']['table']['join'] = array('users' => array(
'table' => 'my_stuff',
'left_field' => 'uid',
'field' => 'uid',
'left_table' => 'users',
),
);
and not getting anywhere.
Now I just need to figure out why.
Also, why is the code you've given me less flexible than using a relationship in the UI?
What flexibility/elegance/performance do I lose this way?
I'm not challenging your statement. I really want to learn.
What you were doing with the
What you were doing with the code above would expose your field to listings of users rather than expose users on listings of your stuff.
I say that it's more flexible because you might not be pulling in everything else that gets added to the user (or more likely node) by using this approach. If you do the same thing with node you won't get the content fields with this sort of a join but you will with a relationship. In general I think you want to make both available to the user unless it doesn't make sense to do so for some other reason. You'll notice both user and node implementations in views core work both ways.