taxonomy_term_depth.module in Taxonomy Term Depth 8.2
Same filename and directory in other branches
Main module file.
File
taxonomy_term_depth.moduleView source
<?php
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
/**
* @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', []);
$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', [
':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,
])
->condition('tid', $tid)
->execute();
}
$cache[$cache_key] = $depth;
}
return $cache[$cache_key];
}
/**
* Implements hook_entity_update();
*/
function taxonomy_term_depth_entity_update($entity) {
$entity_manager = \Drupal::entityTypeManager();
$entity_type = $entity_manager
->getDefinition('taxonomy_term');
$tablename = $entity_type
->getDataTable();
// Update depth of the item on save
if ($entity
->getEntityTypeId() == 'taxonomy_term') {
/**
* @var $entity \Drupal\taxonomy\Entity\Term
*/
// Only update if depth was changed
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__, []);
$cache_key = $tid;
if (!isset($cache[$cache_key]) || $nocache) {
if (version_compare(Drupal::VERSION, '8.6.0', '>=')) {
$cache[$cache_key] = _taxonomy_term_depth_get_parent_d86($tid);
}
else {
$cache[$cache_key] = _taxonomy_term_depth_get_parent_legacy_d85($tid);
}
}
return $cache[$cache_key];
}
/**
* Gets parent of the term
*
* @param $tid
* Term tid to find its parent
*/
function _taxonomy_term_depth_get_parent_d86($tid) {
$tid = Drupal::database()
->query("SELECT parent_target_id FROM {taxonomy_term__parent} WHERE entity_id = :tid", [
':tid' => $tid,
])
->fetchField();
return $tid;
}
/**
* Gets parent of the term
*
* @param $tid
* Term tid to find its parent
*/
function _taxonomy_term_depth_get_parent_legacy_d85($tid) {
$tid = Drupal::database()
->query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid", [
':tid' => $tid,
])
->fetchField();
return $tid;
}
/**
* 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__, []);
$cache_key = $tid;
if (!isset($cache[$cache_key]) || $nocache) {
if (version_compare(Drupal::VERSION, '8.6.0', '>=')) {
$cache[$cache_key] = _taxonomy_term_depth_get_child_d86($tid);
}
else {
$cache[$cache_key] = _taxonomy_term_depth_get_child_legacy_d85($tid);
}
}
return $cache[$cache_key];
}
/**
* Gets child of the term
*
* @param $tid
* Term tid to find its parent
*/
function _taxonomy_term_depth_get_child_d86($tid) {
$tid = Drupal::database()
->query("SELECT entity_id FROM {taxonomy_term__parent} WHERE parent_target_id = :tid", [
':tid' => $tid,
])
->fetchField();
return $tid;
}
/**
* Gets child of the term
*
* @param $tid
* Term tid to find its parent
*/
function _taxonomy_term_depth_get_child_legacy_d85($tid) {
$tid = Drupal::database()
->query("SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent = :tid", [
':tid' => $tid,
])
->fetchField();
return $tid;
}
/**
* @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 = [];
$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 [
'api' => 3,
];
}
/**
* Implements hook_views_data_alter().
*/
function taxonomy_term_depth_views_data_alter(array &$data) {
$data['taxonomy_term_field_data']['depth_level'] = [
'title' => t('Depth'),
'group' => t('Taxonomy term'),
'help' => t('Add depth value to sort and filter.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'help' => t('Filter by the depth value.'),
'id' => 'numeric',
],
];
return $data;
}
/**
* Implements hook_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;
}
/**
* 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);
}
/**
* Implements hook_entity_operation_alter().
*/
function taxonomy_term_depth_entity_operation(EntityInterface $entity) {
$entity_type_id = $entity
->getEntityTypeId();
if ($entity_type_id == 'taxonomy_vocabulary') {
$operations = [];
$entity_id = $entity
->id();
$operations['taxonomy_term_depth_update'] = [
'title' => t('Update term depths'),
'weight' => 26,
'url' => Url::fromRoute('taxonomy_term_depth.update_depth_form', [
'taxonomy_vocabulary' => $entity_id,
]),
];
}
return $operations;
}