Question

This is a Views 6.x-2.x problem: On a site with many different views (many of which are blocks included in Panels that pass arguments to blocks) I would like to filter views by a taxonomy term depending on the domain the site is visited through. This filtering should be additional to a first argument (taxonomy term).

The site is configured to work with different domains, let's say example1.com and example2.com. I want to "connect" those domains to the taxonomy terms 45 and 115.

So for example:

example1.com/my_view/1 Should show all nodes that have term 1 and term 45.

example2.com/my_view/1 Should show all nodes that have term 1 and term 115.

My approach was to add a second argument (the first is the default taxonomy term ID argument). As default argument I use the following snipped in the argument handling code:

<?php
// Get domain.
$host = preg_match('/[^.]+\.[^.]+$/', $_SERVER['HTTP_HOST'], $hit);
$host = $hit[0];

// Select taxonomy term.
if ($host == 'example1.com'){
  $taxonomy = '45';
} elseif ($host == 'example2.com'){
  $taxonomy = '115';
}

return $taxonomy;
?>

This works when I use a page display with the path my_view/% (making only the first argument mandatory). But when I use it in a panel, I just get an empty view (if "no context" is chosen) or the second argument doesn't have any effect (if "term id of first/all term" is chosen).

Any ideas what could be wrong? I have really tried a lot.

Was it helpful?

Solution 2

As I found out here, views ignores the second argument if the first is not present. So setting the following default argument for the first taxonomy argument solves the problem, although it's more of a workaround than a real solution:

if (arg(0) != 'taxonomy') {
  return 'all';
} else {
  return arg(2);
}

OTHER TIPS

If you have a custom module you can use hook_views_query_alter. You basically pick out the "where" clause that's almost doing what you want it to and override it with your custom criteria.

function [custom module name]_views_query_alter(&$view, &$query) {
  // pick out the right View by its name
  if($view->name == "[your View's machine name]"){

    // drupal_set_message(print_r($query->where, 1)); // Uncomment to see "where" array

    // Get domain.
    $host = preg_match('/[^.]+\.[^.]+$/', $_SERVER['HTTP_HOST'], $hit);
    $host = $hit[0];

    // Change the taxonomy term dependent on host
    if ($host == 'example1.com'){
      $query->where[0]['clauses'][2] = "(term_node_value_1.tid = 45)";
    } elseif ($host == 'example2.com'){
      $query->where[0]['clauses'][2] = "(term_node_value_1.tid = 115)";
    }
  }
}

You'll have to examine the $query object to determine which clause to override and the names of the variables involved - uncomment the drupal_set_message line to see it. This technique allows you to do all sorts of tricky exceptions that wouldn't be possible with Views alone. Clear your cache after you put this code in your module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top