Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Taxonomy Term Depth

2,897 sites Security covered
View on drupal.org

This module automatically adds a "depth" property to taxonomy terms, indicating their level within a hierarchy. It updates this depth when terms are saved or moved and includes a tool to calculate depths for existing terms.

This module adds crucial property called "depth" which indicates the term depth with a value 1, 2, 3 and further if needed. It automatically changes taxonomy term depth on save or update. Also there is a batch to automatically calculate and set all term depths if you are installing this on existing project.

To-Do:

  1. Better integration with Views: field, filter, sort

This is now in beta state so it is for code usage only. Please create issues, argue with me, tell your ideas and of course PATCHES ARE WELCOME!

Here is short api documentation of useful functions:

taxonomy_term_depth_get_by_tid($tid, $force = FALSE);

Gets term's depth value in digital representation
$tid is term id which depth needs to be get
$force (optional) when set to TRUE, the depth will be recalculated again using SQL queries and database record will be updated with new value. For example, if you changed its parent using plain SQL update query, you can easily call this function with $force=true and depth value will be updated in database

function taxonomy_term_depth_get_chain($tid, $reveresed = FALSE);

Returns an array containing all parents of this term
$tid is the identifier of term whose parent we need to get
$reversed (optional) if set to TRUE the return array will be reversed

Examples:

  Term level 1 [tid=1]
  --Term level 2 [tid=2]
  ----Term level 3 [tid=3]

> echo taxonomy_term_depth_get_by_tid(3);
> 3

> echo taxonomy_term_depth_get_by_tid(1):
> 1


> print_r(taxonomy_term_depth_get_parents(1));
> array()

> print_r(taxonomy_term_depth_get_parents(3));
> array(2, 1)

> print_r(taxonomy_term_depth_get_full_chain(1));
> array(1, 2, 3)

> print_r(taxonomy_term_depth_get_full_chain(1, TRUE));
> array(3, 2, 1)

Drupal 8 Highlights

Looks like the name depth have conflicts if using it as field name, so there is property depth_level

// To get level from Term object use field depth_level
$depth = $entity->depth_level->first() ? $entity->depth_level->first()->value : NULL;

Activity

Total releases
5
First release
Feb 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 5
Maintenance
Dormant

Release Timeline

Releases

Version Type Release date
3.0.1 Stable Apr 2, 2025
3.0.1-rc1 Pre-release Mar 19, 2025
3.0.0 Stable Mar 5, 2025
3.0.0-rc1 Pre-release Feb 19, 2025
3.0.0-beta1 Pre-release Feb 5, 2025