I am trying to make the value of a string (name) found in mymodule_names.name available to Views by means of hook_views_data().

To get the name, I need to first look up the nid of the node I want to view in a table named mymodule. This tells me the email-address of user who authored the node. Then I need to look up the email address in the table mymodule_names, by finding the row where the same email address is in the column mymodule_names.email. What I want returned is the user name attached to that email-address, which the value of mymodule_names.name in the same row.

The code below is how far I have been able to get. This makes the value of mymodule_names.email available - but getting that is just an intermediary step. I think I need a second join to get the user name. I am unable to figure out how to do do that. How should I modify this code to get the data from the second table?

 * Implements hook_views_data().
function mymodule_views_data() {
  $data = array();
  $data['mymodule']['table']['group'] = t('MyModule');
  $data['mymodule']['table']['base'] = array(
    // This is the identifier field for the view.
    'field' => 'nid',
    'title' => t('MyModule'),
    'help' => t('Table contains ...'),
    'weight' => -10,

  // This table references the {node} table. The declaration below creates an
  // 'implicit' relationship to the node table, so that when 'node' is the base
  // table, the fields are automatically available.
  $data['mymodule']['table']['join'] = array(
    'node' => array(
      // Primary key
      'left_field' => 'nid',
      // Foreign key
      'field' => 'nid',
  $data['mymodule']['nid'] = array(
  'title' => t('Title'),
    'help' => t('Help.'),
  // Join with the email field.
  $data['mymodule']['email'] = array(
    'title' => t('Email'),
    'help' => t('Author email.'),
    'field' => array(
      'handler' => 'views_handler_field',
      // This is used by the table display plugin.
      'click sortable' => TRUE,
    'sort' => array(
      'handler' => 'views_handler_sort',
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    'argument' => array(
      'handler' => 'views_handler_argument_string',
  return $data;

(This is a non-standard setup where node.uid cannot be used to get the node author UID.)

I know how to get the name by inserting a subquery with hook_views_query_alter() (see: this answer). The question is about doing without using subquery.

1 Answer 1

$data['mymodule']['table']['join']['node'] = [
  //Second table for Join
  'left_table' => 'foo',
  'left_field' => 'nid',
  'field' => 'nid',

more info: https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_data/8.6.x

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.