Commit fa394894 authored by chr.fritsch's avatar chr.fritsch Committed by borisson_

Issue #3005018 by chr.fritsch, gnikolovski, tunic, borisson_: Improve...

Issue #3005018 by chr.fritsch, gnikolovski, tunic, borisson_: Improve performance for taxonomy hierarchical facets
parent ac6b4dcc
......@@ -17,6 +17,20 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class Taxonomy extends HierarchyPluginBase {
/**
* Static cache for the nested children.
*
* @var array
*/
protected $nestedChildren = [];
/**
* Static cache for the term parents.
*
* @var array
*/
protected $termParents = [];
/**
* The term storage.
*
......@@ -69,6 +83,10 @@ class Taxonomy extends HierarchyPluginBase {
* {@inheritdoc}
*/
public function getNestedChildIds($id) {
if (isset($this->nestedChildren[$id])) {
return $this->nestedChildren[$id];
}
$children = $this->termStorage->loadChildren($id);
$children = array_filter(array_values(array_map(function ($it) {
return $it->id();
......@@ -78,7 +96,7 @@ class Taxonomy extends HierarchyPluginBase {
foreach ($children as $child) {
$subchilds = array_merge($subchilds, $this->getNestedChildIds($child));
}
return array_merge($children, $subchilds);
return $this->nestedChildren[$id] = array_merge($children, $subchilds);
}
/**
......@@ -106,11 +124,15 @@ class Taxonomy extends HierarchyPluginBase {
* Returns FALSE if no parent is found, else parent tid.
*/
protected function taxonomyGetParent($tid) {
if (isset($this->termParents[$tid])) {
return $this->termParents[$tid];
}
$parents = $this->termStorage->loadParents($tid);
if (empty($parents)) {
return FALSE;
}
return reset($parents)->id();
return $this->termParents[$tid] = reset($parents)->id();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment