2

i'm trying to hook all my site views with hook_views_query_alter()

I can't find how to add a relationship and condition in the query.

Here is my (not working) code

/**
 * Implementation of hook_views_api().
 */
function taxonomy_location_views_api() { // your module name into hook_views_api
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'taxonomy_location'),
  );
}

/**
 * Implementation of hook_views_query_alter().
 */
function taxonomy_location_views_query_alter(&$view, &$query) {
  // get taxonomy term ID
  $tid = subdomain_get_sid(subdomain_get_current_subdomain());
  if($tid) {
    $join = new views_join;
    $join->construct('term_node', 'node', 'nid', 'nid', array(), 'INNER');
    $query->add_relationship('term_node', $join, 'node');
    $query->add_where('term_node', 'term_node.tid', $tid);
  }
}

Any ideas?

3

I found a slightly different solution that works.

Instead of adding programatically the relation, I instead added a clause in $query->where

function taxonomy_location_views_query_alter(&$view, &$query) {
  $tid = subdomain_get_sid(subdomain_get_current_subdomain());
  if($tid) {
    $query->where[0]['clauses'][] = "(node.vid IN (SELECT tn.vid FROM term_node tn WHERE tn.tid = %d))";
    $query->where[0]['args'][] = $tid;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.