Skip to content
Snippets Groups Projects

Issue #2928661 by kriboogh: Reordering taxonomy terms after "Reset to...

4 files
+ 481
68
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -2,6 +2,7 @@
namespace Drupal\taxonomy;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Entity\Sql\TableMappingInterface;
@@ -71,7 +72,9 @@ public function create(array $values = []) {
if (empty($values['parent'])) {
$values['parent'] = [0];
}
$entity = parent::create($values);
return $entity;
}
@@ -85,6 +88,7 @@ public function resetCache(?array $ids = NULL) {
$this->treeTerms = [];
$this->trees = [];
$this->vocabularyHierarchyType = [];
parent::resetCache($ids);
}
@@ -107,6 +111,7 @@ public function updateTermHierarchy(EntityInterface $term) {
*/
public function loadParents($tid) {
$terms = [];
/** @var \Drupal\taxonomy\TermInterface $term */
if ($tid && $term = $this->load($tid)) {
foreach ($this->getParents($term) as $id => $parent) {
@@ -191,6 +196,7 @@ protected function getAncestors(TermInterface $term) {
}
}
}
return $this->ancestors[$term->id()];
}
@@ -215,6 +221,7 @@ public function getChildren(TermInterface $term) {
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(TRUE)
->condition('parent', $term->id());
return static::loadMultiple($query->execute());
}
@@ -223,6 +230,7 @@ public function getChildren(TermInterface $term) {
*/
public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
$cache_key = implode(':', func_get_args());
if (!isset($this->trees[$cache_key])) {
// We cache trees, so it's not CPU-intensive to call on a term and its
// children, too.
@@ -315,6 +323,7 @@ public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities =
}
$this->trees[$cache_key] = $tree;
}
return $this->trees[$cache_key];
}
@@ -327,6 +336,7 @@ public function nodeCount($vid) {
$query->leftJoin($this->getBaseTable(), 'td', '[ti].[tid] = [td].[tid]');
$query->condition('td.vid', $vid);
$query->addTag('vocabulary_node_count');
return $query->execute()->fetchField();
}
@@ -338,6 +348,22 @@ public function resetWeights($vid) {
->fields(['weight' => 0])
->condition('vid', $vid)
->execute();
// Reset terms for this vocabulary. We don't use loadMultiple for
// performance reasons and limit memory consumption.
$entity_type_key = $this->getEntityType()->getKey('bundle');
$term_ids = $this->getQuery()
->accessCheck(FALSE)
->condition($entity_type_key, $vid)
->execute();
$this->resetCache($term_ids);
// Invalidate correct cache tags.
array_walk($term_ids, function (&$tid) {
$tid = 'taxonomy_term:' . $tid;
});
Cache::invalidateTags($term_ids);
}
/**
@@ -352,15 +378,18 @@ public function getNodeTerms(array $nids, array $vids = [], $langcode = NULL) {
$query->orderby('td.name');
$query->condition('tn.nid', $nids, 'IN');
$query->addTag('taxonomy_term_access');
if (!empty($vids)) {
$query->condition('td.vid', $vids, 'IN');
}
if (!empty($langcode)) {
$query->condition('td.langcode', $langcode);
}
$results = [];
$all_tids = [];
foreach ($query->execute() as $term_record) {
$results[$term_record->node_nid][] = $term_record->tid;
$all_tids[] = $term_record->tid;
@@ -368,11 +397,13 @@ public function getNodeTerms(array $nids, array $vids = [], $langcode = NULL) {
$all_terms = $this->loadMultiple($all_tids);
$terms = [];
foreach ($results as $nid => $tids) {
foreach ($tids as $tid) {
$terms[$nid][$tid] = $all_terms[$tid];
}
}
return $terms;
}
@@ -453,6 +484,7 @@ public function __sleep() {
$vars = parent::__sleep();
// Do not serialize static cache.
unset($vars['ancestors'], $vars['treeChildren'], $vars['treeParents'], $vars['treeTerms'], $vars['trees'], $vars['vocabularyHierarchyType']);
return $vars;
}
@@ -461,6 +493,7 @@ public function __sleep() {
*/
public function __wakeup() {
parent::__wakeup();
// Initialize static caches.
$this->ancestors = [];
$this->treeChildren = [];
Loading