Commit 29559424 authored by alexpott's avatar alexpott

Issue #1976298 by larowlan: Move taxonomy_get_tree() and associated functions...

Issue #1976298 by larowlan: Move taxonomy_get_tree() and associated functions to Taxonomy storage, deprecate procedural wrappers.
parent ffde907c
......@@ -31,26 +31,16 @@ class Overview extends OverviewTerms {
/**
* Constructs a \Drupal\forum\Form\OverviewForm object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInteface $module_handler
* The module handler service.
*/
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($module_handler);
public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) {
parent::__construct($module_handler, $entity_manager);
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
......
......@@ -463,7 +463,7 @@ public function resetCache() {
* {@inheritdoc}
*/
public function getParents($tid) {
return taxonomy_term_load_parents_all($tid);
return $this->entityManager->getStorage('taxonomy_term')->loadAllParents($tid);
}
/**
......
......@@ -7,6 +7,7 @@
namespace Drupal\taxonomy\Form;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -25,14 +26,24 @@ class OverviewTerms extends FormBase {
*/
protected $moduleHandler;
/**
* The term storage controller.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $storageController;
/**
* Constructs an OverviewTerms object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) {
$this->moduleHandler = $module_handler;
$this->storageController = $entity_manager->getStorage('taxonomy_term');
}
/**
......@@ -40,7 +51,8 @@ public function __construct(ModuleHandlerInterface $module_handler) {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler')
$container->get('module_handler'),
$container->get('entity.manager')
);
}
......@@ -97,9 +109,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular
$delta = 0;
$term_deltas = array();
// @todo taxonomy_get_tree needs to be converted to a service and injected.
// Will be fixed in http://drupal.org/node/1976298.
$tree = taxonomy_get_tree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
$tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
$tree_index = 0;
do {
// In case this tree is completely empty.
......
This diff is collapsed.
......@@ -37,11 +37,22 @@ public function updateTermHierarchy(EntityInterface $term);
* @param int $tid
* Term ID to retrieve parents for.
*
* @return array
* @return \Drupal\taxonomy\TermInterface[]
* An array of term objects which are the parents of the term $tid.
*/
public function loadParents($tid);
/**
* Finds all ancestors of a given term ID.
*
* @param int $tid
* Term ID to retrieve ancestors for.
*
* @return \Drupal\taxonomy\TermInterface[]
* An array of term objects which are the ancestors of the term $tid.
*/
public function loadAllParents($tid);
/**
* Finds all children of a term ID.
*
......@@ -50,7 +61,7 @@ public function loadParents($tid);
* @param string $vid
* An optional vocabulary ID to restrict the child search.
*
* @return array
* @return \Drupal\taxonomy\TermInterface[]
* An array of term objects that are the children of the term $tid.
*/
public function loadChildren($tid, $vid = NULL);
......@@ -60,11 +71,22 @@ public function loadChildren($tid, $vid = NULL);
*
* @param string $vid
* Vocabulary ID to retrieve terms for.
* @param int $parent
* The term ID under which to generate the tree. If 0, generate the tree
* for the entire vocabulary.
* @param int $max_depth
* The number of levels of the tree to return. Leave NULL to return all
* levels.
* @param bool $load_entities
* If TRUE, a full entity load will occur on the term objects. Otherwise
* they are partial objects queried directly from the {taxonomy_term_data}
* table to save execution time and memory consumption when listing large
* numbers of terms. Defaults to FALSE.
*
* @return array
* @return \Drupal\taxonomy\TermInterface[]
* An array of term objects that are the children of the vocabulary $vid.
*/
public function loadTree($vid);
public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
/**
* Count the number of nodes in a given vocabulary ID.
......
......@@ -66,7 +66,7 @@ function testTermIndentation() {
$this->assertNoPattern('|<div class="indentation">&nbsp;</div>|');
// Check explicitly that term 2 has no parents.
drupal_static_reset();
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$parents = taxonomy_term_load_parents($term2->id());
$this->assertTrue(empty($parents), 'Term 2 has no parents now');
}
......
......@@ -433,9 +433,7 @@ function testTermReorder() {
// Fetch the created terms in the default alphabetical order, i.e. term1
// precedes term2 alphabetically, and term2 precedes term3.
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->id(), 0, NULL, TRUE);
$this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
......@@ -460,9 +458,7 @@ function testTermReorder() {
);
$this->drupalPostForm(NULL, $edit, t('Save'));
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$terms = taxonomy_get_tree($this->vocabulary->id());
$this->assertEqual($terms[0]->tid, $term2->id(), 'Term 2 was moved above term 1.');
$this->assertEqual($terms[1]->parents, array($term2->id()), 'Term 3 was made a child of term 2.');
......@@ -474,14 +470,12 @@ function testTermReorder() {
// Ensure form redirected back to overview.
$this->assertUrl('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$terms = taxonomy_get_tree($this->vocabulary->id(), 0, NULL, TRUE);
$this->assertEqual($terms[0]->id(), $term1->id(), 'Term 1 was moved to back above term 2.');
$this->assertEqual($terms[1]->id(), $term2->id(), 'Term 2 was moved to back below term 1.');
$this->assertEqual($terms[2]->id(), $term3->id(), 'Term 3 is still below term 2.');
$this->assertEqual($terms[2]->parents, array($term2->id()), 'Term 3 is still a child of term 2.' . var_export($terms[1]->id(), 1));
$this->assertEqual($terms[2]->parents, array($term2->id()), 'Term 3 is still a child of term 2.');
}
/**
......
......@@ -389,41 +389,22 @@ function taxonomy_vocabulary_get_names() {
* @return
* An array of term objects which are the parents of the term $tid, or an
* empty array if parents are not found.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\taxonomy\TermStorageController::loadParents()
*/
function taxonomy_term_load_parents($tid) {
$parents = &drupal_static(__FUNCTION__, array());
if ($tid && !isset($parents[$tid])) {
$tids = \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid);
$parents[$tid] = entity_load_multiple('taxonomy_term', $tids);
}
return isset($parents[$tid]) ? $parents[$tid] : array();
return \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid);
}
/**
* Find all ancestors of a given term ID.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\taxonomy\TermStorageController::loadAllParents()
*/
function taxonomy_term_load_parents_all($tid) {
$cache = &drupal_static(__FUNCTION__, array());
if (isset($cache[$tid])) {
return $cache[$tid];
}
$parents = array();
if ($term = entity_load('taxonomy_term', $tid)) {
$parents[] = $term;
$n = 0;
while ($parent = taxonomy_term_load_parents($parents[$n]->id())) {
$parents = array_merge($parents, $parent);
$n++;
}
}
$cache[$tid] = $parents;
return $parents;
return \Drupal::entityManager()->getStorage('taxonomy_term')->loadAllParents($tid);
}
/**
......@@ -435,16 +416,12 @@ function taxonomy_term_load_parents_all($tid) {
* @return
* An array of term objects that are the children of the term $tid, or an
* empty array when no children exist.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\taxonomy\TermStorageController::loadChildren()
*/
function taxonomy_term_load_children($tid) {
$children = &drupal_static(__FUNCTION__, array());
if ($tid && !isset($children[$tid])) {
$tids = \Drupal::entityManager()->getStorage('taxonomy_term')->loadChildren($tid);
$children[$tid] = entity_load_multiple('taxonomy_term', $tids);
}
return isset($children[$tid]) ? $children[$tid] : array();
return \Drupal::entityManager()->getStorage('taxonomy_term')->loadChildren($tid);
}
/**
......@@ -468,91 +445,12 @@ function taxonomy_term_load_children($tid) {
* to have "depth" and "parents" attributes in addition to its normal ones.
* Results are statically cached. Term objects will be partial or complete
* depending on the $load_entities parameter.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\taxonomy\TermStorageController::loadTree()
*/
function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
$children = &drupal_static(__FUNCTION__, array());
$parents = &drupal_static(__FUNCTION__ . ':parents', array());
$terms = &drupal_static(__FUNCTION__ . ':terms', array());
// We cache trees, so it's not CPU-intensive to call taxonomy_get_tree() on a
// term and its children, too.
if (!isset($children[$vid])) {
$children[$vid] = array();
$parents[$vid] = array();
$terms[$vid] = array();
$result = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($result as $term) {
$children[$vid][$term->parent][] = $term->tid;
$parents[$vid][$term->tid][] = $term->parent;
$terms[$vid][$term->tid] = $term;
}
}
// Load full entities, if necessary. The entity storage caches the results.
if ($load_entities) {
$term_entities = entity_load_multiple('taxonomy_term', array_keys($terms[$vid]));
}
$max_depth = (!isset($max_depth)) ? count($children[$vid]) : $max_depth;
$tree = array();
// Keeps track of the parents we have to process, the last entry is used
// for the next processing step.
$process_parents = array();
$process_parents[] = $parent;
// Loops over the parent terms and adds its children to the tree array.
// Uses a loop instead of a recursion, because it's more efficient.
while (count($process_parents)) {
$parent = array_pop($process_parents);
// The number of parents determines the current depth.
$depth = count($process_parents);
if ($max_depth > $depth && !empty($children[$vid][$parent])) {
$has_children = FALSE;
$child = current($children[$vid][$parent]);
do {
if (empty($child)) {
break;
}
$term = $load_entities ? $term_entities[$child] : $terms[$vid][$child];
if (isset($parents[$vid][$load_entities ? $term->id() : $term->tid])) {
// Clone the term so that the depth attribute remains correct
// in the event of multiple parents.
$term = clone $term;
}
$term->depth = $depth;
unset($term->parent);
$tid = $load_entities ? $term->id() : $term->tid;
$term->parents = $parents[$vid][$tid];
$tree[] = $term;
if (!empty($children[$vid][$tid])) {
$has_children = TRUE;
// We have to continue with this parent later.
$process_parents[] = $parent;
// Use the current term as parent for the next iteration.
$process_parents[] = $tid;
// Reset pointers for child lists because we step in there more often
// with multi parents.
reset($children[$vid][$tid]);
// Move pointer so that we get the correct term the next time.
next($children[$vid][$parent]);
break;
}
} while ($child = next($children[$vid][$parent]));
if (!$has_children) {
// We processed all terms in this hierarchy-level, reset pointer
// so that this function works the next time it gets called.
reset($children[$vid][$parent]);
}
}
}
return $tree;
return \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, $parent, $max_depth, $load_entities);
}
/**
......
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