Creating link with user name given uid

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
mathieso's picture

This must be easy, but I've spent much time with no joy. Feeling thick headed.

Got a table defined like this:

<?php
  $schema
['learningexercise_user_solutions'] = array(
   
'description' => t('Stores user\'s solutions.'),
   
'fields' => array(
     
'nid' => array(
       
'type' => 'int',
...      ),
     
'vid' => array(
       
'type' => 'int',
...      ),
     
'uid' => array(
       
'type' => 'int',
...      ),
     
'solution' => array(
       
'description' => t('The text of the solution.'),
       
'type' => 'text',
...      ),
...
?>

uid is a user, not the creator of the node with id in nid, but someone else. I'd like to let Views show a link to the user's account, with the user name as the text of the link.

Here's some code from hook_views_data:

<?php
  $data
['learningexercise_user_solutions']['table']['group'] = t('Learning exercise');
 
$data['learningexercise_user_solutions']['table']['join'] = array(
     
'users' => array(
       
'left_field' => 'uid',
       
'field' => 'uid',
      ),
     
'node' => array(
       
'left_field' => 'nid',
       
'field' => 'nid',
      ),
  );
 
$data['learningexercise_user_solutions']['uid'] =
      array(
       
'title' => t('Solving user'),
       
'help' => t('The user who created the solution.'),

       
'field' => array(
         
'handler' => 'views_handler_field_user',
         
'click sortable' => TRUE,
        ),
       
'sort' => array(
         
'handler' => 'views_handler_sort',
        ),
       
'relationship' => array(
         
'base' => 'users',
         
'left_field' => 'uid',
         
'field' => 'uid',
         
'handler' => 'views_handler_relationship',
         
'label' => t('User'),
        ),
      );
//End uid
?>

This shows a link, but with the uid as the text, rather than the name.

Tried various handlers in the field array, like views_handler_field_user_link, but with no success.

Was able to create a custom handler that gets called correctly, and could put a user_load() call in there, but that would run a lot of extra queries for no good reason.

Any advice? I must be missing something simple.

Kieran

Comments

something simple

Pasqualle's picture

remove the whole field definition, as you have already defined a join to users table.

use the "User: Name" field to display the user name..

Similar problem

ellis-'s picture

I have a similar problem.
I have more then 1 uid relations from a child table similar to his.

<?php
  $schema
['project'] = array(
   
'description' => 'Project node extra data',
   
'fields' => array(
     
'vid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE,
       
'default' => 0,
      ),
     
'nid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE,
       
'default' => 0,
      ),
      
    

     
'custrep_uid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => FALSE
      
),
      
      
'project_manager_uid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => FALSE
      
),
      
      
'production_manager_uid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => FALSE
      
),
      
      
'client_contact_uid' => array(
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => FALSE
      
),
      
     
    ),
   
'indexes' => array(
      
'nid' => array('nid'),
    ),
   
'primary key' => array('vid', 'nid'),

  );
?>

How would i go about doing that. My views_data looks more or less like OPs.

Erik Ellis
The Farm Stockholm

Never mind

ellis-'s picture

I figured it out.

It was as simple as adding a relations field.

<?php
$data
['project']['custrep_uid'] = array(
   
'title' => t('Customer representative'),
   
'help' => '...',
   
'relationship' => array(
       
'base' => 'users',
       
'field' => 'uid',
       
'handler' => 'views_handler_relationship',
       
'label' => t('Customer representative'),
    ),
  );
?>

Erik Ellis
The Farm Stockholm

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week