Is there going to be a relationship available for userreference fields in views with base 'user'?

If not already planned, I'd like to request this feature.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Er, there should be already ?
Last time I checked, it worked well, I think...

casey’s picture

In dev version I'm sure? I am using 6.x-2.1 and it's not available in that version, at least not for me...

Didn't check 6.x-2.x-dev really. But I thought, I mention it anyway.

But when it's in the dev version, you may close this issue.

yched’s picture

Category: feature » support

No, it's in 2.1.
Of course, you need to be building a *node* view to have the relationship available (and you also need to actually have a userfield on one of your content types).

Other than that, I'm not sure why you would not see it...

casey’s picture

Category: feature » support

Ah, I get it; what I means is:

I want the reverse relationship to be available. So we can create views that lists users that occur in userreference fields. this allows things like:

  • usage of fields from contenttypes that have userreference fields in views that use base 'user' (listings of users)
  • add arguments/filters for views with base 'user' (e.g. a list of users that are referenced in userreference field A of contenttype B where field C = x)
  • use these views in advanced views settings of userreference fields

So that isn't possible yet? I think this would be a nice feature.

casey’s picture

Category: support » feature

So this is a feature request again :)

casey’s picture

Category: support » feature

Add to userreference_field_settings($op = 'views data') something like:

      // Relationship: Add a relationship for related userreference field.
      $data['users'][$field['field_name'] .'_vid'] = array(
        'group' => t('Node'),
        'title' => $data[$table_alias][$field['field_name'] .'_uid']['title'],
        'help' => t('Create a relationship to a node with an userreference field.'),
        'relationship' => array(
          'relationship field' => 'uid',
          'base' => $db_info['table'],
          'base field' => $db_info['columns']['uid']['column'],
          'label' => t($field['widget']['label']),
          'content_field_name' => $field['field_name'],
        ),
      );

But now we don't have access to the node yet. I am not sure how to do this. Probably a new relationship handler is needed.

casey’s picture

Update:

It works when the following is added to userreference_field_settings($op = 'views data'):


      // Join: join node table with users table through field's table.
      $data['node_'. $table_alias]['table']['join']['users'] = array(
        'table' => 'node',
        'field' => 'vid',
        'left_table' => $table_alias,
        'left_field' => 'vid',
      );
      // Join: join field's table with users table.
      $data[$table_alias]['table']['join']['users'] = array(
        'table' => $db_info['table'],
        'field' => $db_info['columns']['uid']['column'],
        'left_field' => 'uid',
      );
      // Relationship: Add a relationship for related node.
      $data['users'][$field['field_name'] .'_vid'] = array(
        'group' => t('Node'),
        'title' => $data[$table_alias][$field['field_name'] .'_uid']['title'],
        'title_short' => $data[$table_alias][$field['field_name'] .'_uid']['title_short'],
        'help' => t('Create a relationship to a node with this userreference field.'),
        'relationship' => array(
          'base' => 'node',
          'base field' => 'vid',
          'relationship table' => $table_alias,
          'relationship field' => 'vid',
          'handler' => 'content_handler_relationship',
          'label' => t($field['widget']['label']),
          'content_field_name' => $field['field_name'],
        ),
      );

Only issue is that the "userreference -> user" relationship is available also; not really an issue, but it might be confusing.

  • The "userreference.uid -> user.uid" relationship was normally only available in views with type "Node" and gives access to all users that are referenced in a userreference field.
  • The new "user.uid -> node[userreference.uid]": "In order to join to the users table, the node table must first link to a userreference table, and they join on the 'vid' field."
  • The "userreference.uid -> user.uid" relationship is now also available in views with type "User" and relates all siblings (plus the user itself) of a user in a userreference field.

Hard to explain, but I think it should be clear.

Please integrate this feature. (There are others who are looking for this, e.g. http://acquia.com/node/17697)

casey’s picture

Somebody already had a look at this?

casey’s picture

Component: Views Integration » userreference.module
mrfelton’s picture

Code in #7 works for me.

NikLP’s picture

Subscribing and hopefully trying this out.

NikLP’s picture

Have patched the module with the code provided. The relationship is indeed available to join the base user table to the node table via a user reference field. However, if you have more than one such field in existence, only the last created one appears as an available option in the "select a relationship handler"-type dialog in Views.

mrfelton and I proved this by deleting the last created reference field, and lo and behold a different field was available in its place.

At first we thought that there was a problem in the patch between single and multiple value fields, but this does not appear to be the case, so consider that bit tested.

joachim’s picture

Subscribe.

joachim’s picture

Status: Active » Needs review
FileSize
2.65 KB

The problem isn't that the code is wrong.

The problem is that the code returns an array like this for each field:

node_data_field_user_refs_other  (Array, 3 elements) 
users_node_data_field_user_refs_other  (Array, 1 element) 
node_node_data_field_user_refs_other  (Array, 1 element) 
users  (Array, 1 element) 

This is returned by userreference_field_settings($op = 'views data'), and all these arrays get merged to hand over to Views... and so the last field to return this clobbers all the others because they all have a 'user' key.

The fix is to change that array_merge to an array_merge_recursive.

Here's a patch which includes the code from #7 and the array fix.

I'm not sure why we're getting the relationships called "Content: FIELDNAME" -- as far as I can tell they're meaningless on a user view. I think that bit of the code needs a 'skip base' setting. The addition of the new code makes those appear whereas they didn't before, but the missing 'skip base' is a problem in existing code so out of the scope of this patch.

NikLP’s picture

Tested this patch and it seems fine, I can now see all the user ref fields as relationships and arguments as expected. No clue about Jo's other comments regarding skip base, but that's another matter...

NikLP’s picture

Status: Needs review » Reviewed & tested by the community
helmo’s picture

While searching for something similar I found this issue and tried out the patch from #14.
It seems fine for making a reverse relationship list.

I did get stuck when I tried to add another cck filed linked to the node pulled in through the reverse reference. This gave me duplicate results, one of which correct. The distinct setting de-duplicates this, but does not return the right record.
But I think that's just asking for a messy join.

mr1’s picture

Tested the patch.. Works like a charm. Thanks!

NikLP’s picture

Probably needs re-rolling against the new userreference.module as CCK has been updated...?

EDIT: Actually no it doesn't. Obviously if the ur.module file has been updated, you need to reapply the patch, but I just applied it again with no problem, FYI.

casey’s picture

Project: Content Construction Kit (CCK) » References
Version: 6.x-2.x-dev » 7.x-2.x-dev
Component: userreference.module » Code: user_reference
Status: Reviewed & tested by the community » Needs work
pfaocle’s picture

Bit late I guess, but I'm currently using #14 on a site with cck 6.x-2.9. Working OK so far.

presleyd’s picture

Using #14 now on CCK-6.x-3.0-alpha3 and it's working fine from what I can tell. Should this really be moved to References already? Granted I have some D7 sites and I'd like this functionality there but this patch seems like it could have been committed to CCK for D6 and then a new issue be created for References on D7? Perhaps I'm thinking that through clearly enough however.

yched’s picture

Status: Needs work » Closed (duplicate)

backreference relationships have just been committed to References D7, in #1083902: Backreferencing (listing nodes that reference the target node). For D6, they are provided in the Reverse Node Reference module - http://drupal.org/project/reverse_node_reference.

Thus, marking as 'duplicate'

pvhee’s picture

Status: Closed (duplicate) » Needs work

Reverse Node Reference only supports backreferences from node to node, and is clear about not including user->node backreferences (see #1139116: Could this support User reverse references?), so this issue is technically not a duplicate and I believe there is still a need for this and the patch from #14.

chingis’s picture

I used patch #14 on 6.x-2.9 and it works fine, but I can't add fields from this back referenced content-type