Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

taxonomy_term_depth

2,795 sites Security covered
View on drupal.org

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
Release cadence
14 days
Stability
40% stable

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