Taxonomy view representative node contextual filter leads to incorrect results I have a view that is outputting incorrectly, perhaps not according to the code, but according to what I need it to do. So, a challenge?

The view lists colors (taxonomy terms from vocabulary 'Color') which have content assigned to them.

View type:      Taxonomy
Format:         Unformatted list
Show:           Fields
Fields:         Taxonomy term: Name
Relationships:  Taxonomy term: Representative node (required)
Filters:        Taxonomy term: Vocabulary (= Color)


The site must display different content depending on which area the user lives in. There is an 'Area' vocabulary, currently with two terms: East (tid = 1) and West (tid = 2).

Because of this, all relevant content types have an "Area" term reference field which can either be set to "East" or "West".

To get this to work, I added a contextual filter for the view.

Type: (Representative node) Content: Has taxonomy term ID.


A fixed value comes from this PHP:

$url = explode('/', $_SERVER['REQUEST_URI']);
$tid = 0;

if($url[1] == 'east') {
  $tid = 1;
} elseif($url[1] == 'west') {
  $tid = 2;
}

return $tid;


Values that don't validate cause the view to be hidden.

The problem: Taxonomy terms with content assigned to 'East' are being hidden from the list. I know that there is content using these 'Color' terms, but they are being excluded.

Here is what I think is happening:


View is executed for path 'domain/east'.
Representative nodes for each 'Color' taxonomy term are selected.
Contextual filter is applied to the representative node for each term - if the single representative node does NOT have the correct value for the URL (eg. 1, 'East' in this case) it is excluded.
Views output displays trimmed results, which are not representative of the actual data.


Does anyone know how I could get it to work properly? I'm hoping to avoid creating views for each area, as later, adding 'North', 'North West', 'South', 'South South East', etc, would get very frustrating. Ideally I'd be able to use a hook. 

For example, would I be able to alter the representative node query, to get the contextual filter and change the SQL based on that?

Thanks for reading. :-)
First off do NOT put PHP code in the fixed value. This gets stored in the db which is a bad idea. Instead use taxonomy term ID from URL for the default value.

If that does not work as is use a hook. I would probably use hook_views_pre_view in this case. You can use the same code you have in your example to change your $args or $view-&gt;args can't remember which off the top of my head.