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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.