Views: Display all nodes that have sibling terms (i.e. terms under the same parent) .

While working on an extra requirement for metalera.gr, we needed to do the following:

When viewing node/xxx, we would like to display all nodes that have terms that belong to the same parent as the one we currently visit. Let's say for example, that we have the following parent:

Colors

--red

--blue

--green

Node/xxx has the term green assigned. We need our view to display all nodes that have red or blue as well. 

To achieve that, we first create a new view, then:  

Add an argument in our view. Select the Term:tid option

In the 'Action to take if argument is not present' select the 'Provide default argument'

The 'Default argument type' select PHP Code

In the PHP argument code, fill in the following

$vocab_required = array('1');

$node = node_load(arg(1));

if ($node) {

foreach($node->taxonomy as $term) {

if (in_array($term->vid,$vocab_required)) {

$parents = taxonomy_get_parents($term->tid);

$parent = array_shift($parents);

$children = taxonomy_get_children($parent->tid,1);

$terms = array();

foreach ($children as $child) {

$terms[] = $child->tid;   

}

print_r ($terms);

return implode('+',$terms);

}

}

}

else { return; }

Be careful not to include the <?php ?> tags. 

After that, 

6. Check the "Allow multiple terms per argument" checkbox

7. Check the "Allow multiple arguments to work together"

That's it! Our view will now return all nodes that have terms that belong to the same parent. 

Tags: 
Drupal 6, Taxonomy

Recent Comments

Forward by Dries Buytaert, Founder and Project Lead, Drupal; CTO Acquia.

Order now from:

Managing Expectations Podcast with DrupalEasy

Author Interview http://tinyurl.com/8a8nxno

We provide the most Shiny Drupal Bits & Pixels

Since Drupal 4.x