Skip to content
Snippets Groups Projects
Commit 5ad1df5e authored by Joris Vercammen's avatar Joris Vercammen Committed by Joris Vercammen
Browse files

Issue #2919034 by borisson_, Christian.wiedemann, Berdir, larowlan:...

Issue #2919034 by borisson_, Christian.wiedemann, Berdir, larowlan: FacetsHierarchy Taxonomy uses old (taxonomy_term_hierachy) table
parent 77734ea7
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,8 @@
namespace Drupal\facets\Plugin\facets\hierarchy;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\facets\Hierarchy\HierarchyPluginBase;
use Drupal\taxonomy\TermStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -19,11 +18,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class Taxonomy extends HierarchyPluginBase {
/**
* The current primary database.
* The term storage.
*
* @var \Drupal\Core\Database\Connection
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $database;
protected $termStorage;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
......@@ -34,12 +33,12 @@ class Taxonomy extends HierarchyPluginBase {
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* The current primary database.
* @param \Drupal\taxonomy\TermStorageInterface $termStorage
* The term storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, TermStorageInterface $termStorage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
$this->termStorage = $termStorage;
}
/**
......@@ -50,7 +49,7 @@ class Taxonomy extends HierarchyPluginBase {
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database')
$container->get('entity_type.manager')->getStorage('taxonomy_term')
);
}
......@@ -70,38 +69,30 @@ class Taxonomy extends HierarchyPluginBase {
* {@inheritdoc}
*/
public function getNestedChildIds($id) {
$children = &drupal_static(__FUNCTION__, []);
if (!isset($children[$id])) {
$query = $this->database->select('taxonomy_term_hierarchy', 'h');
$query->addField('h', 'tid');
$query->condition('h.parent', $id);
$queried_children = $query->execute()->fetchCol();
$subchilds = [];
foreach ($queried_children as $child) {
$subchilds = array_merge($subchilds, $this->getNestedChildIds($child));
}
$children[$id] = array_merge($queried_children, $subchilds);
$children = $this->termStorage->loadChildren($id);
$children = array_filter(array_values(array_map(function ($it) {
return $it->id();
}, $children)));
$subchilds = [];
foreach ($children as $child) {
$subchilds = array_merge($subchilds, $this->getNestedChildIds($child));
}
return isset($children[$id]) ? $children[$id] : [];
return array_merge($children, $subchilds);
}
/**
* {@inheritdoc}
*/
public function getChildIds(array $ids) {
$result = $this->database->select('taxonomy_term_hierarchy', 'th')
->fields('th', ['tid', 'parent'])
->condition('th.parent', '0', '>')
->condition((new Condition('OR'))
->condition('th.tid', $ids, 'IN')
->condition('th.parent', $ids, 'IN')
)
->execute();
$parents = [];
foreach ($result as $record) {
$parents[$record->parent][] = $record->tid;
foreach ($ids as $id) {
$terms = $this->termStorage->loadChildren($id);
$parents[$id] = array_filter(array_values(array_map(function ($it) {
return $it->id();
}, $terms)));
}
$parents = array_filter($parents);
return $parents;
}
......@@ -115,15 +106,11 @@ class Taxonomy extends HierarchyPluginBase {
* Returns FALSE if no parent is found, else parent tid.
*/
protected function taxonomyGetParent($tid) {
$parent = &drupal_static(__FUNCTION__, []);
if (!isset($parent[$tid])) {
$query = $this->database->select('taxonomy_term_hierarchy', 'h');
$query->addField('h', 'parent');
$query->condition('h.tid', $tid);
$parent[$tid] = $query->execute()->fetchField();
$parents = $this->termStorage->loadParents($tid);
if (empty($parents)) {
return FALSE;
}
return isset($parent[$tid]) ? $parent[$tid] : FALSE;
return reset($parents)->id();
}
}
......@@ -30,6 +30,7 @@ class FacetTest extends KernelTestBase {
*/
public static $modules = [
'facets',
'taxonomy',
];
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment