Praxent

Visualizing Drupal Taxonomy Relationships in a Flat Hierarchy

Visualizing Drupal Taxonomy Relationships in a Flat Hierarchy

By Steve Williams

You may need to visually represent a flat hierarchy to render a full relationship. You may also need to import and export content with a flat hierarchy in order to maintain the proper parent-child relationships. One method of defining relationships in Drupal is with taxonomies. When working with Drupal 7’s Taxonomy API, you will find it does have a method for listing terms and their parents. In order to provide a list of terms with their parents, you will have to perform multiple Taxonomy API calls.

Let’s look at an example of two brands of pants with similar children.

Example of vocabulary with hierarchy:

If we visually represent the above vocabulary in a flat hierarchy list it will look like this…

In order to flatten our vocabulary, we first pull the taxonomy tree of our vocabulary. In this example, our vocabulary has an id of 1.

$terms = taxonomy_get_tree('1');

The above code will return an array with depth and parent information.

  [tid] => 2    [vid] => 1    [name] => Jeans    [description] =>    [weight] => 0    [depth] => 1    [parents] => Array      (        [0] => 1      )
We can obtaining the parent id from taxonomy_get_tree() and target terms with depths of “1” and “2”.$output = "";
foreach ($terms as $term) {    // Skip terms with depth of 0.    if ($term->parents[0] != '0') {      // Perform processing on term with depth 1.      if ($term->depth == '1') {        // Obtain term name of parent term.        $parentterm = taxonomy_get_term($term->parents[0]);        $output .=  $parentterm->name . ", " . $term->name . "<br />";      }      if ($term->depth == '2') {        // Perform processing on term with depth 2.      }    }  }  return $output;

The above code will output “Levi’s, Jeans” & “Wrangler, Jeans,” but we need to also print our hierarchy with one more depth. To process terms with a depth of 2 we need to perform a lookup using taxonomy_get_parents().

// Process terms with depth of 2.

if ($term->depth == '2'){    // Obtain term name of parent term just like on terms with depth of 1.    $parentterm = taxonomy_get_term($term->parents[0]);    // Obtain top parent of parent term.    $topterms = taxonomy_get_parents($parentterm->tid);    // Loop through and print results.    foreach ($topterms as $topterm) {      $output .= $topterm->name . "," . $parentterm->name . ",";      $output .= $term->name . "<br />";    }  }

Putting it all together:

function mymodule_termlist() {    $terms = taxonomy_get_tree('1');    $output = “”;    foreach ($terms as $term) {      // Skip terms with depth of 0.      if ($term->parents[0] != '0') {        // Perform processing on term with depth 1.        if ($term->depth == '1') {          // Obtain term name of parent term.          $parentterm = taxonomy_get_term($term->parents[0]);          $output .=  $parentterm->name . ", " . $term->name . "<br />";        }        // Process term with depth 2.        if ($term->depth == '2') {          // Obtain term name of parent term          // just like on terms with depth of 1.          $parentterm = taxonomy_get_term($term->parents[0]);          // Obtain topparent of parent term.          $topterms = taxonomy_get_parents($parentterm->tid);          // Loop through terms and print results.          foreach($topterms as $topterm) {            $output .= $topterm->name . "," . $parentterm->name . ",";            $output .= $term->name . "<br />";          }        }       }    }    return $output;  }

Our visual result will render as…

Levie’s, Jeans
Levie’s, Jeans, 501
Wrangler, Jeans
Wrangler, Jeans, Original

Visualizing a flat hierarchy provides the user with an easy way to quickly scan relationships in the related content. It also provides a method to export and import content while maintaining the proper relationships.

Want to learn more? Check out our web app development services for more information on how you can get started on your next project, today.