• About
        • About
          • Overview
          • What to Expect
          • Careers
          • Team
          • CANDO Culture
          • FAQ
        • Praxent Pricing Guide

          To give you an idea of what investment looks like we've created a guide with estimates by product type as well as set-priced starter engagements.

          Download Now
  • Industries
        • Industries
          • Fintech
          • Insurance
          • Lending
          • Wealth Management
          • Real Estate
          • Other
        • Praxent Pricing Guide

          To give you an idea of what investment looks like we've created a guide with estimates by product type as well as set-priced starter engagements.

          Download Now
  • Services
    • Design
      • User Experience Design
      • Customer Journey Mapping
      • Design Sprints
      • UX Audit
      • User Testing
      • User Research
    • Development
      • Custom Software Development
      • Application Modernization
      • Mobile App Development
      • Web App Development
      • Web Portal Development
      • Front End Development
      • Backend Development
      • Cloud Deployment
      • Implementations
      • Staff Augmentation
  • Case Studies
  • Insights
  • Schedule a Call
  • About
    • About
    • Overview
    • Careers
    • CANDO Culture
    • What to Expect
    • Team
    • FAQ
    • #
  • Industries
    • Industries
    • Fintech
    • Insurance
    • Lending
    • Wealth Management
    • Real Estate
    • Other
    • #
  • Services
    • Services
    • Design
      • User Experience Design
      • Customer Journey Mapping
      • Design Sprints
      • UX Audit
      • User Research
      • User Testing
    • Development
      • Custom Software Development
      • Application Modernization
      • Mobile App Development
      • Web App Development
      • Web Portal Development
      • Frontend Development
      • Backend Development
      • Cloud Deployment
      • Implementations
      • Staff Augmentation
    • #
  • Case Studies
  • Insights
  • Contact

Speak with an expert

(512) 553-6830
Close menu

Topics

  • Uncategorized
  • Development
  • Life at Praxent
  • Project Management
  • Strategy
  • UX & Design
  • Tech & Business News
  • Mobile
  • Product Management
  • Featured
  • Financial Services Innovators
  • Awards
  • UX Insights

Types

  • article Article
  • podcastpodcast
  • presspress
  • webinarwebinar
  • whitepaperwhitepaper

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:

  • Levi’s
    • Jeans
      • 501
  • Wranglers
    • Jeans
      • Original

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

  • Levi’s, Jeans, 501
  • Wrangler, Jeans, Original

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Featured

Praxent Introduces Accelerator App, Helping WealthTechs Speed Time to Market 

Praxent Introduces Accelerator App, Helping WealthTechs Speed Time to Market 

Fintech product agency launches starter app to help wealthtechs accelerate innovation. Praxent Introduces Accelerator … Read More

What the Kardashians can teach your FI about fintech partners to identify niche markets.

What the Kardashians can teach your FI about fintech partners to identify niche markets.

Read more

The 4 Reasons Software Modernizations Fail (and 12 Strategies for Success)

The 4 Reasons Software Modernizations Fail (and 12 Strategies for Success)

We share the strategies you’ll need to modernize your online customer experience so you can outperform your competitor...Read more

Many companies have built software applications that no longer meet customer expectations. We help financial services companies modernize those applications so they can remain relevant against born-digital competitors.

4330 Gaines Ranch Loop, Suite 230
Austin, TX 78735

(512) 553-6830

hello@praxent.com

DESIGN
  • UX Design
  • Design Sprints
  • User Research
  • User Testing
DEVELOP
  • Custom Software
  • Web Portals
  • App Modernization
  • Web Apps
  • Mobile Apps
ABOUT
  • Case Studies
  • Team
  • Culture
  • Careers
  • Insights
  • Contact

© 2023 Praxent
Privacy Terms Site Map