You are here

function _tft_get_depth in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \_tft_get_depth()

Get the depth of the term.

Parameters

int $tid: The taxonomy term tid.

Return value

int The depth of the term, or 0 if no valid term tid was given.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to _tft_get_depth()
ReorderFolderForm::buildForm in src/Form/ReorderFolderForm.php
Form constructor.
ReorderFolderForm::manage_folders_form in src/Form/ReorderFolderForm.php
Form helper. Flattens the terms tree and creates the form elements.

File

./tft.module, line 159
Contains tft.module.

Code

function _tft_get_depth($tid) {
  static $cache = [];
  if (!$tid) {
    return 0;
  }
  if (isset($cache[$tid])) {
    return $cache[$tid];
  }
  $term = Term::load($tid);
  if (!$term) {
    return 0;
  }
  $depth = -1;
  $pid = $tid;

  /** @var \Drupal\taxonomy\TermStorage $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  while ($pid) {
    $depth++;
    $result = $storage
      ->loadParents($pid);
    $result = reset($result);
    $pid = empty($result) ? FALSE : $result
      ->id();
  }
  $cache[$tid] = $depth;
  return $depth;
}