Commit 5ad1df5e authored by borisson_'s avatar borisson_ Committed by borisson_

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
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
namespace Drupal\facets\Plugin\facets\hierarchy; namespace Drupal\facets\Plugin\facets\hierarchy;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\facets\Hierarchy\HierarchyPluginBase; use Drupal\facets\Hierarchy\HierarchyPluginBase;
use Drupal\taxonomy\TermStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -19,11 +18,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -19,11 +18,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class Taxonomy extends HierarchyPluginBase { 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. * Constructs a Drupal\Component\Plugin\PluginBase object.
...@@ -34,12 +33,12 @@ class Taxonomy extends HierarchyPluginBase { ...@@ -34,12 +33,12 @@ class Taxonomy extends HierarchyPluginBase {
* The plugin_id for the plugin instance. * The plugin_id for the plugin instance.
* @param mixed $plugin_definition * @param mixed $plugin_definition
* The plugin implementation definition. * The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\taxonomy\TermStorageInterface $termStorage
* The current primary database. * 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); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database; $this->termStorage = $termStorage;
} }
/** /**
...@@ -50,7 +49,7 @@ class Taxonomy extends HierarchyPluginBase { ...@@ -50,7 +49,7 @@ class Taxonomy extends HierarchyPluginBase {
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('database') $container->get('entity_type.manager')->getStorage('taxonomy_term')
); );
} }
...@@ -70,38 +69,30 @@ class Taxonomy extends HierarchyPluginBase { ...@@ -70,38 +69,30 @@ class Taxonomy extends HierarchyPluginBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getNestedChildIds($id) { public function getNestedChildIds($id) {
$children = &drupal_static(__FUNCTION__, []); $children = $this->termStorage->loadChildren($id);
if (!isset($children[$id])) { $children = array_filter(array_values(array_map(function ($it) {
$query = $this->database->select('taxonomy_term_hierarchy', 'h'); return $it->id();
$query->addField('h', 'tid'); }, $children)));
$query->condition('h.parent', $id);
$queried_children = $query->execute()->fetchCol(); $subchilds = [];
$subchilds = []; foreach ($children as $child) {
foreach ($queried_children as $child) { $subchilds = array_merge($subchilds, $this->getNestedChildIds($child));
$subchilds = array_merge($subchilds, $this->getNestedChildIds($child));
}
$children[$id] = array_merge($queried_children, $subchilds);
} }
return isset($children[$id]) ? $children[$id] : []; return array_merge($children, $subchilds);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getChildIds(array $ids) { 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 = []; $parents = [];
foreach ($result as $record) { foreach ($ids as $id) {
$parents[$record->parent][] = $record->tid; $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; return $parents;
} }
...@@ -115,15 +106,11 @@ class Taxonomy extends HierarchyPluginBase { ...@@ -115,15 +106,11 @@ class Taxonomy extends HierarchyPluginBase {
* Returns FALSE if no parent is found, else parent tid. * Returns FALSE if no parent is found, else parent tid.
*/ */
protected function taxonomyGetParent($tid) { protected function taxonomyGetParent($tid) {
$parent = &drupal_static(__FUNCTION__, []); $parents = $this->termStorage->loadParents($tid);
if (empty($parents)) {
if (!isset($parent[$tid])) { return FALSE;
$query = $this->database->select('taxonomy_term_hierarchy', 'h');
$query->addField('h', 'parent');
$query->condition('h.tid', $tid);
$parent[$tid] = $query->execute()->fetchField();
} }
return isset($parent[$tid]) ? $parent[$tid] : FALSE; return reset($parents)->id();
} }
} }
...@@ -30,6 +30,7 @@ class FacetTest extends KernelTestBase { ...@@ -30,6 +30,7 @@ class FacetTest extends KernelTestBase {
*/ */
public static $modules = [ public static $modules = [
'facets', 'facets',
'taxonomy',
]; ];
/** /**
......
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