I have a content type 'contest' that users submit guesses on. The guesses are entities that have uid and nid properties (and of course their own primary key, gid) to record the user and the contest to which they belong.

How can I display only contests on which a given user has submitted guesses? A similar problem might be how to return only nodes of a certain type that a given user has commented on.

By using this within hook_views_data and setting the query to distinct (which I'd also prefer not to have to do) within the Views UI, I was able to confine the results to nodes that have guesses whose nid property match - but I can't figure out how to confine the results to nodes with guess entities whose nid AND uid properties match:

$data['node']['nid'] = array(
    'relationship' => array(
        'handler'        => 'views_handler_relationship',
        'base'           => 'entity_guess',
        'base field'     => 'nid',
        'title'          => t('Has guesses'),
        'label'          => t('contests with guesses'),

Thanks so much!

  • Is this doable? I know it must be. I just can't find any great documentation on the Views API
    – tvanc
    Oct 23, 2012 at 20:22

1 Answer 1


I ended up achieving the result that I wanted with hook_views_query_alter:

function site_views_query_alter (& $view, & $query)
  if ('block_3' == $view->current_display) {
    $account        = SiteUser::loadByID(arg(1), TRUE);
    $query->where[] = array(
      'conditions' => array(
          'field'    => 'node.nid',
          // Return just the IDs
          'value'    => SiteContest::getContestsPlayedBy($account, TRUE),
          'operator' => 'in',
      'args'       => array(),
      'type'       => 'AND',

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.