I have a situation where the colour scheme of a node depends upon which taxonomy terms have been assigned to it.

However, if a node has more than 1 parent taxonomy term assigned to it, this is a certain colour scheme of itself. I'm not sure of the technical name but by parent taxonomy term I mean the top level taxonomy term, as there will only ever be 2 levels deep, so I call this parent taxononomy term.

So I need a certain html class to be created for these special nodes who have more than one taxonomy term assigned to them.

I was thinking even to use a hidden field and to write a module which checks how many parent taxonomy terms have been assigned before creating or saving this content type in question, and then simply adds a value to this hidden field, I could then use this field to add a class to my content in views.

Am I allowed to ask for suggestions here for this, I am sorry if this is against rules

1 Answer 1


There's no need to store this information when it can be calculated on the fly.

The taxonomy module contains a function called taxonomy_get_parents() that can retrieve the parents for a given term. Using this, you can iterate over the node field values to look for any root ("parent") level terms (i.e. they have no parent) and count the number of terms accordingly.

// Given a node object & taxonomy field machine name, return count of root terms
function mymodule_node_root_term_count($node, $field_name) {
  $count = 0;

  // $node->{$field_name}[LANGUAGE_NONE] will vary with multi-lingual enviroment
  foreach($node->{$field_name}[LANGUAGE_NONE] as $term) {
    $parents = taxonomy_get_parents($term['tid']);
    // A root term will return an empty array.
    $count += empty($parents) ? 1 : 0;

  return $count;

Once you've built your logic for determining the type of node, you can utilize it in a theme preprocess function to add CSS classes accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.