2
votes

I am working with Drupal Views, that filter our content based on geography, and shows news from a specific municipality.

Our taxonomy is hierarchical:

  • Regions -- Municipalities from the region --- Towns from the municipality

For example:

  • Region A (region) -- Municipality X (municipality) --- Town 1 --- Town 2 -- Municipality Y (municipality) --- Town 3 --- Town 4 -- Municipality Z (municipality) --- Town 5 --- Town 6

We tag our nodes with town names, and by using taxonomy term id as argument in our view, we can easily list all stories from any municipality.

Now we would like to add a new feature to our view: Also list news from the neighbouring municipality. Municiplaity Y is a Neghbour of Municipality Z, and we added this relation to the taxonomy term.

So now we can show all stories from Y by choosing using Taxonomy Depth argument. We can also show all stories, by using the Taxonomy Related terms argument.

But how to show all nodes from both Y and its sibling Z with views?

1

1 Answers

0
votes

You should change the validator of the views argument to PHP code. Then, in the validator field you will be able to change the argument dynamically. This way, having the tid you can get all the related terms.

  • set the Action to take if argument is not present to "display empty text" ;
  • on validator options, set the validator to "PHP Code";
  • in the code:
    • get the tid using arg(1);
    • use taxonomy_get_related($tid) to get the related terms;
    • build a string in with the syntax "tid+related_tid+related_tid+related_tid" to return as argument;
    • redefine $handler->argument as the built string;
  • finally, set the Allow multiple terms per argument as True

Here's an example code to insert in the php code validation form:

$tid = arg(1);
$result = strval($tid);
$related = taxonomy_get_related($tid);
foreach($related as $i){
  if (intval($i)>0){
    $result.="+".$i;
  }
}
$handler->argument = $result;
return $result;