You are here

function taxonomy_term_depth_get_by_tid in Taxonomy Term Depth 7

Same name and namespace in other branches
  1. 8.2 taxonomy_term_depth.module \taxonomy_term_depth_get_by_tid()
  2. 8 taxonomy_term_depth.module \taxonomy_term_depth_get_by_tid()

@todo: Provide description

Parameters

$tid:

bool $force:

Return value

int

5 calls to taxonomy_term_depth_get_by_tid()
DynamicDepthCalculationTest::testCalculateDepth in ./taxonomy_term_depth.test
Creates and ensures that a feed is unique, checks source, and deletes feed.
DynamicDepthCalculationTest::_testDepthCache in ./taxonomy_term_depth.test
DynamicDepthCalculationTest::_testDepthProperty in ./taxonomy_term_depth.test
taxonomy_term_depth_batch_callbacks_update_term_depth in ./taxonomy_term_depth.batch.inc
taxonomy_term_depth_entity_update in ./taxonomy_term_depth.module
Implements hook_entity_update();

File

./taxonomy_term_depth.module, line 37
Provides some custom functionality.

Code

function taxonomy_term_depth_get_by_tid($tid, $force = FALSE) {
  $cache =& drupal_static('taxonomy_term_depth', array());
  $cache_key = $tid;
  if ($force || !isset($cache[$cache_key])) {

    // Try to get cached value first but only if no need to rebuild
    // If force flag is set to TRUE the query won't be executed
    if ($force || !($depth = db_query('SELECT depth FROM {taxonomy_term_data} WHERE tid=:tid', array(
      ':tid' => $tid,
    ))
      ->fetchField())) {

      // Calculate value without using caches
      $depth = _taxonomy_term_depth_get_nocache($tid);

      // And write to database cache
      db_update('taxonomy_term_data')
        ->fields(array(
        'depth' => $depth,
      ))
        ->condition('tid', $tid)
        ->execute();
    }
    $cache[$cache_key] = $depth;
  }
  return $cache[$cache_key];
}