summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorborisson_2018-01-18 19:13:13 (GMT)
committerJoris Vercammen2018-01-18 19:13:13 (GMT)
commit5ad1df5e763c0ba01aa5ce7efe2329cf73952ac9 (patch)
tree7201c62d771ed5deb19f806f01cdcb49ad815a9c
parent77734ea7ae3c74940cf9bbf40bc179e30a8257b4 (diff)
Issue #2919034 by borisson_, Christian.wiedemann, Berdir, larowlan: FacetsHierarchy Taxonomy uses old (taxonomy_term_hierachy) table8.x-1.0-beta2
-rw-r--r--src/Plugin/facets/hierarchy/Taxonomy.php69
-rw-r--r--tests/src/Kernel/Entity/FacetTest.php1
2 files changed, 29 insertions, 41 deletions
diff --git a/src/Plugin/facets/hierarchy/Taxonomy.php b/src/Plugin/facets/hierarchy/Taxonomy.php
index 410cbcf..478dc4c 100644
--- a/src/Plugin/facets/hierarchy/Taxonomy.php
+++ b/src/Plugin/facets/hierarchy/Taxonomy.php
@@ -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();
}
}
diff --git a/tests/src/Kernel/Entity/FacetTest.php b/tests/src/Kernel/Entity/FacetTest.php
index 03c5636..4d68bff 100644
--- a/tests/src/Kernel/Entity/FacetTest.php
+++ b/tests/src/Kernel/Entity/FacetTest.php
@@ -30,6 +30,7 @@ class FacetTest extends KernelTestBase {
*/
public static $modules = [
'facets',
+ 'taxonomy',
];
/**