You are here

taxonomy_term_depth.module in Taxonomy Term Depth 8

Same filename and directory in other branches
  1. 8.2 taxonomy_term_depth.module
  2. 7 taxonomy_term_depth.module

Main module file.

File

taxonomy_term_depth.module
View source
<?php

/**
 * @file
 * Main module file.
 */

/**
 * Require all constants
 */
require_once __DIR__ . '/constants.inc';

/**
 * @todo: Provide description
 * @param $tid
 * @param bool $force
 * @return int
 */
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 = Drupal::database()
      ->query('SELECT depth_level FROM {taxonomy_term_field_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
      Drupal::database()
        ->update('taxonomy_term_field_data')
        ->fields([
        'depth_level' => $depth,
        'depth' => $depth,
      ])
        ->condition('tid', $tid)
        ->execute();
    }
    $cache[$cache_key] = $depth;
  }
  return $cache[$cache_key];
}

/**
 * Implements hook_entity_update();
 */
function taxonomy_term_depth_entity_update($entity) {

  // Update depth of the item on save
  if ($entity
    ->getEntityTypeId() == 'taxonomy_term') {

    /**
     * @var $entity \Drupal\taxonomy\Entity\Term
     */

    // Only update if depth was changed
    // @fixme: Uncomment this condition since v2.0

    //if (NULL === $entity->depth_level->first() || $entity->depth_level->first()->value != _taxonomy_term_depth_get_nocache($entity->id())) {
    $depth = taxonomy_term_depth_get_by_tid($entity
      ->id(), TRUE);
    $entity->depth_level
      ->setValue([
      'value' => $depth,
    ]);

    //}
  }
}

/**
 * Implements hook_entity_insert()
 */
function taxonomy_term_depth_entity_insert($entity) {
  taxonomy_term_depth_entity_update($entity);
}

/**
 * Calculates taxonomy term depth from database
 * @param $tid
 * @return int
 */
function _taxonomy_term_depth_get_nocache($tid) {
  $parent = taxonomy_term_depth_get_parent($tid);
  if (!$parent) {
    return 1;
  }
  else {
    return 1 + _taxonomy_term_depth_get_nocache($parent);
  }
}

/**
 * Gets parent of the term
 * @param $tid
 *  Term tid to find its parent
 */
function taxonomy_term_depth_get_parent($tid, $nocache = FALSE) {
  $cache =& drupal_static(__FUNCTION__, array());
  $cache_key = $tid;
  if (!isset($cache[$cache_key]) || $nocache) {
    $cache[$cache_key] = Drupal::database()
      ->query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid", array(
      ':tid' => $tid,
    ))
      ->fetchField();
  }
  return $cache[$cache_key];
}

/**
 * Gets child of the term
 * @param $tid
 *  Term tid to find its parent
 */
function taxonomy_term_depth_get_child($tid, $nocache = FALSE) {
  $cache =& drupal_static(__FUNCTION__, array());
  $cache_key = $tid;
  if (!isset($cache[$cache_key]) || $nocache) {
    $cache[$cache_key] = Drupal::database()
      ->query("SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent = :tid", array(
      ':tid' => $tid,
    ))
      ->fetchField();
  }
  return $cache[$cache_key];
}

/**
 * @param $tid
 * @return array
 * @deprecated
 */
function taxonomy_term_depth_get_chain($tid, $reversed = FALSE) {
  return taxonomy_term_depth_get_parents($tid, $reversed);
}

/**
 * Get parents of the term.
 * @param $tid
 * @return array
 */
function taxonomy_term_depth_get_parents($tid, $reversed = FALSE) {

  // @todo: Caching parents or not worth?
  $parents = array();
  $parent = $tid;
  while ($parent = taxonomy_term_depth_get_parent($parent)) {
    $parents[] = $parent;
  }
  return $reversed ? array_reverse($parents) : $parents;
}

/**
 * Gets children of the term.
 * @param $tid
 * @return array
 */
function taxonomy_term_depth_get_children($tid, $reversed = FALSE) {
  $children = [];

  // Now get children
  $child = $tid;
  while ($child = taxonomy_term_depth_get_child($child)) {
    $children[] = $child;
  }
  return $reversed ? array_reverse($children) : $children;
}

/**
 * Gets full chain of terms, including term itself
 * @param $tid
 * @return array
 */
function taxonomy_term_depth_get_full_chain($tid, $reversed = FALSE) {
  $parents = taxonomy_term_depth_get_parents($tid, TRUE);
  $children = taxonomy_term_depth_get_children($tid, TRUE);
  $chain = array_merge($parents, [
    $tid,
  ], $children);
  return $reversed ? array_reverse($chain) : $chain;
}

/**
 * Implements hook_views_api().
 */
function taxonomy_term_depth_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_views_data_alter().
 */
function taxonomy_term_depth_views_data_alter(array &$data) {
  $data['taxonomy_term_field_data']['depth'] = array(
    'title' => t('Depth (deprecated. Use depth_level)'),
    'group' => t('Taxonomy term'),
    'help' => t('Add depth value to sort and filter.'),
    'field' => array(
      'id' => 'numeric',
    ),
    'sort' => array(
      'id' => 'standard',
    ),
    'filter' => array(
      'help' => t('Filter by the depth value.'),
      'id' => 'numeric',
    ),
  );
  $data['taxonomy_term_field_data']['depth_level'] = array(
    'title' => t('Depth'),
    'group' => t('Taxonomy term'),
    'help' => t('Add depth value to sort and filter.'),
    'field' => array(
      'id' => 'numeric',
    ),
    'sort' => array(
      'id' => 'standard',
    ),
    'filter' => array(
      'help' => t('Filter by the depth value.'),
      'id' => 'numeric',
    ),
  );
  return $data;
}

/**
 * Implements hook_term_depth_entity_base_field_info().
 */
function taxonomy_term_depth_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() == 'taxonomy_term') {

    // $field_db = \Drupal\Core\Database\Database::getConnection()->schema();
    $fields['depth_level'] = \Drupal\Core\Field\BaseFieldDefinition::create('integer')
      ->setProvider('taxonomy_term_depth')
      ->setLabel(t('Depth'))
      ->setDescription(t('Term depth (1 based)'))
      ->setDefaultValue(NULL);
  }
  return $fields;
}

///**

// * Implements hook_term_depth_entity_base_field_info_alter().
// */

//function taxonomy_term_depth_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {

//  /**
//   * @var $fields\Drupal\Core\Field\BaseFieldDefinition[]
//   */
//  if ($entity_type->id() == 'taxonomy_term' && isset($fields['depth_level'])) {
//    // Replace exact column name.
//    $fields['depth_level']->setName('depth');
//  }

//}

/**
 * Get QueueManager service.
 * @return Drupal\taxonomy_term_depth\QueueManager\Manager.
 */
function taxonomy_term_depth_queue_manager($vid = NULL) {
  return \Drupal::service('taxonomy_term_depth.queue_service')
    ->setVid($vid);
}

Functions

Namesort descending Description
taxonomy_term_depth_entity_base_field_info Implements hook_term_depth_entity_base_field_info().
taxonomy_term_depth_entity_insert Implements hook_entity_insert()
taxonomy_term_depth_entity_update Implements hook_entity_update();
taxonomy_term_depth_get_by_tid @todo: Provide description
taxonomy_term_depth_get_chain
taxonomy_term_depth_get_child Gets child of the term
taxonomy_term_depth_get_children Gets children of the term.
taxonomy_term_depth_get_full_chain Gets full chain of terms, including term itself
taxonomy_term_depth_get_parent Gets parent of the term
taxonomy_term_depth_get_parents Get parents of the term.
taxonomy_term_depth_queue_manager Get QueueManager service.
taxonomy_term_depth_views_api Implements hook_views_api().
taxonomy_term_depth_views_data_alter Implements hook_views_data_alter().
_taxonomy_term_depth_get_nocache Calculates taxonomy term depth from database