1

I have a custom table (myusersession) which contains the uid field that is a reference to user entities.

How can I add user entity fields (exactly 'name') to my view?

I tried many variations of join, in hook_views_data(), but none of them works.

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

  $data['myusersession']['uid'] = array(
    'title' => t('UID'),
    'help' => t(''),
    'field' => array(
      'id' => 'numeric',
    ),
    'filter' => array(
      'id' => 'numeric',
    ),
    'argument' => array(
      'id' => 'numeric',
    ),
    'relationship' => array(
      'title' => t('User'),
      'help' => t(''),
      'base' => 'users',
      'base field' => 'uid',
      'id' => 'standard',
    ),
  );

I also tested variations of the table name, such as users_field_data, users, and user.

As I understand the following code should be work, but it doesn't in my case.

    'relationship' => array(
      'title' => $this->t('User'),
      'label' => $this->t('User'),
      'base' => 'users',
      'base field' => 'uid',
      'relationship field' => 'uid',
    ),    

With ['table']['join'], the table appears as column on the view with User as base, but but if the view base is myusersession, User doesn't appear.

2 Answers 2

1
    //Relationship b/w users_field_data and myusersession
  $data['myusersession']['uid']['relationship'] = [
    'title' => t('User'),
    'help' => t('Relate User.'),
    'id' => 'standard',
    'base' => 'users_field_data',
    'base field' => 'uid',
    'field' => 'uid',
    'label' => t('User'),
  ];
  return $data;

This code will work.

0

Found that there is additional step: you have to add/enable that relationship at View GUI->Advanced->Relationship

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.