Commit 7ef3c8c7 authored by xjm's avatar xjm

Issue #2452577 by JeroenT, AjitS, tadityar, arpitr, dpopdan, Mile23, andypost:...

Issue #2452577 by JeroenT, AjitS, tadityar, arpitr, dpopdan, Mile23, andypost: Remove Usage of deprecated function taxonomy_*
parent 7918441b
......@@ -128,8 +128,8 @@ protected function actions(array $form, FormStateInterface $form_state) {
* A select form element.
*/
protected function forumParentSelect($tid, $title) {
// @todo Inject a taxonomy service when one exists.
$parents = taxonomy_term_load_parents($tid);
$taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
$parents = $taxonomy_storage->loadParents($tid);
if ($parents) {
$parent = array_shift($parents);
$parent = $parent->id();
......@@ -139,8 +139,7 @@ protected function forumParentSelect($tid, $title) {
}
$vid = $this->config('forum.settings')->get('vocabulary');
// @todo Inject a taxonomy service when one exists.
$children = taxonomy_get_tree($vid, $tid, NULL, TRUE);
$children = $taxonomy_storage->loadTree($vid, $tid, NULL, TRUE);
// A term can't be the child of itself, nor of its children.
foreach ($children as $child) {
......@@ -148,8 +147,7 @@ protected function forumParentSelect($tid, $title) {
}
$exclude[] = $tid;
// @todo Inject a taxonomy service when one exists.
$tree = taxonomy_get_tree($vid, 0, NULL, TRUE);
$tree = $taxonomy_storage->loadTree($vid, 0, NULL, TRUE);
$options[0] = '<' . $this->t('root') . '>';
if ($tree) {
foreach ($tree as $term) {
......
......@@ -405,7 +405,7 @@ public function getChildren($vid, $tid) {
return $this->forumChildren[$tid];
}
$forums = array();
$_forums = taxonomy_get_tree($vid, $tid, NULL, TRUE);
$_forums = $this->entityManager->getStorage('taxonomy_term')->loadTree($vid, $tid, NULL, TRUE);
foreach ($_forums as $forum) {
// Merge in the topic and post counters.
if (($count = $this->getForumStatistics($forum->id()))) {
......
......@@ -102,7 +102,7 @@ public function testTaxonomyTerms() {
}
else {
$parents = array();
foreach (taxonomy_term_load_parents($tid) as $parent) {
foreach (\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
$parents[] = (int) $parent->id();
}
$this->assertIdentical($parents, $values['parent']);
......
......@@ -78,7 +78,7 @@ public function testForumPager() {
$this->assertTrue($vid, 'Forum navigation vocabulary ID is set.');
// Look up the general discussion term.
$tree = taxonomy_get_tree($vid, 0, 1);
$tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
$tid = reset($tree)->tid;
$this->assertTrue($tid, 'General discussion term is found in the forum vocabulary.');
......
......@@ -68,10 +68,10 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
// See if any of the term's children are about to be become orphans.
$orphans = array();
foreach (array_keys($entities) as $tid) {
if ($children = taxonomy_term_load_children($tid)) {
if ($children = $storage->loadChildren($tid)) {
foreach ($children as $child) {
// If the term has multiple parents, we don't delete it.
$parents = taxonomy_term_load_parents($child->id());
$parents = $storage->loadParents($child->id());
if (empty($parents)) {
$orphans[] = $child->id();
}
......
......@@ -390,9 +390,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$hierarchy = TAXONOMY_HIERARCHY_DISABLED;
$changed_terms = array();
// @todo taxonomy_get_tree needs to be converted to a service and injected.
// Will be fixed in https://www.drupal.org/node/1976298.
$tree = taxonomy_get_tree($vocabulary->id(), 0, NULL, TRUE);
$tree = $this->storageController->loadTree($vocabulary->id(), 0, NULL, TRUE);
if (empty($tree)) {
return;
......
......@@ -177,7 +177,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
}
else {
if (!empty($this->options['hierarchy']) && $this->options['limit']) {
$tree = taxonomy_get_tree($vocabulary->id(), 0, NULL, TRUE);
$tree = $this->termStorage->loadTree($vocabulary->id(), 0, NULL, TRUE);
$options = array();
if ($tree) {
......
......@@ -21,9 +21,10 @@ class TermForm extends ContentEntityForm {
public function form(array $form, FormStateInterface $form_state) {
$term = $this->entity;
$vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary');
$taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
$vocabulary = $vocab_storage->load($term->bundle());
$parent = array_keys(taxonomy_term_load_parents($term->id()));
$parent = array_keys($taxonomy_storage->loadParents($term->id()));
$form_state->set(['taxonomy', 'parent'], $parent);
$form_state->set(['taxonomy', 'vocabulary'], $vocabulary);
......@@ -34,13 +35,14 @@ public function form(array $form, FormStateInterface $form_state) {
'#weight' => 10,
);
// taxonomy_get_tree and taxonomy_term_load_parents may contain large
// \Drupal\taxonomy\TermStorageInterface::loadTree() and
// \Drupal\taxonomy\TermStorageInterface::loadParents() may contain large
// numbers of items so we check for taxonomy.settings:override_selector
// before loading the full vocabulary. Contrib modules can then intercept
// before hook_form_alter to provide scalable alternatives.
if (!$this->config('taxonomy.settings')->get('override_selector')) {
$parent = array_keys(taxonomy_term_load_parents($term->id()));
$children = taxonomy_get_tree($vocabulary->id(), $term->id());
$parent = array_keys($taxonomy_storage->loadParents($term->id()));
$children = $taxonomy_storage->loadTree($vocabulary->id(), $term->id());
// A term can't be the child of itself, nor of its children.
foreach ($children as $child) {
......@@ -48,7 +50,7 @@ public function form(array $form, FormStateInterface $form_state) {
}
$exclude[] = $term->id();
$tree = taxonomy_get_tree($vocabulary->id());
$tree = $taxonomy_storage->loadTree($vocabulary->id());
$options = array('<' . $this->t('root') . '>');
if (empty($parent)) {
$parent = array(0);
......
......@@ -43,6 +43,9 @@ function testTermIndentation() {
$term2 = $this->createTerm($this->vocabulary);
$term3 = $this->createTerm($this->vocabulary);
// Get the taxonomy storage.
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Indent the second term under the first one.
$edit = array(
'terms[tid:' . $term2->id() . ':0][term][tid]' => 2,
......@@ -56,7 +59,7 @@ function testTermIndentation() {
$this->assertPattern('|<div class="js-indentation indentation">&nbsp;</div>|');
// Check explicitly that term 2's parent is term 1.
$parents = taxonomy_term_load_parents($term2->id());
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertEqual(key($parents), 1, 'Term 1 is the term 2\'s parent');
// Move the second term back out to the root level.
......@@ -73,7 +76,7 @@ function testTermIndentation() {
// Check explicitly that term 2 has no parents.
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$parents = taxonomy_term_load_parents($term2->id());
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertTrue(empty($parents), 'Term 2 has no parents now');
}
......
......@@ -85,6 +85,9 @@ public function testTaxonomyVocabularyTree() {
$term[$i] = $this->createTerm($vocabulary);
}
// Get the taxonomy storage.
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Set the weight on $term[1] so it appears before $term[5] when fetching
// the parents for $term[2], in order to test for a regression on
// \Drupal\taxonomy\TermStorageInterface::loadAllParents().
......@@ -113,11 +116,11 @@ public function testTaxonomyVocabularyTree() {
* ------ term[3] | depth: 3
*/
// Count $term[1] parents with $max_depth = 1.
$tree = taxonomy_get_tree($vocabulary->id(), $term[1]->id(), 1);
$tree = $taxonomy_storage->loadTree($vocabulary->id(), $term[1]->id(), 1);
$this->assertEqual(1, count($tree), 'We have one parent with depth 1.');
// Count all vocabulary tree elements.
$tree = taxonomy_get_tree($vocabulary->id());
$tree = $taxonomy_storage->loadTree($vocabulary->id());
$this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');
// Count elements in every tree depth.
......
......@@ -71,6 +71,9 @@ function testTaxonomyTermHierarchy() {
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
// Get the taxonomy storage.
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Check that hierarchy is flat.
$vocabulary = Vocabulary::load($this->vocabulary->id());
$this->assertEqual(0, $vocabulary->getHierarchy(), 'Vocabulary is flat.');
......@@ -81,22 +84,22 @@ function testTaxonomyTermHierarchy() {
$this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save'));
// Check the hierarchy.
$children = taxonomy_term_load_children($term1->id());
$parents = taxonomy_term_load_parents($term2->id());
$children = $taxonomy_storage->loadChildren($term1->id());
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertTrue(isset($children[$term2->id()]), 'Child found correctly.');
$this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
// Load and save a term, confirming that parents are still set.
$term = Term::load($term2->id());
$term->save();
$parents = taxonomy_term_load_parents($term2->id());
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
// Create a third term and save this as a parent of term2.
$term3 = $this->createTerm($this->vocabulary);
$term2->parent = array($term1->id(), $term3->id());
$term2->save();
$parents = taxonomy_term_load_parents($term2->id());
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertTrue(isset($parents[$term1->id()]) && isset($parents[$term3->id()]), 'Both parents found successfully.');
}
......@@ -109,6 +112,8 @@ function testTaxonomyTermChildTerms() {
$term1 = $this->createTerm($this->vocabulary);
$terms_array = '';
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Create 40 terms. Terms 1-12 get parent of $term1. All others are
// individual terms.
for ($x = 1; $x <= 40; $x++) {
......@@ -121,8 +126,8 @@ function testTaxonomyTermChildTerms() {
$edit['parent'] = $term1->id();
}
$term = $this->createTerm($this->vocabulary, $edit);
$children = taxonomy_term_load_children($term1->id());
$parents = taxonomy_term_load_parents($term->id());
$children = $taxonomy_storage->loadChildren($term1->id());
$parents = $taxonomy_storage->loadParents($term->id());
$terms_array[$x] = Term::load($term->id());
}
......@@ -232,7 +237,7 @@ function testNodeTermCreationAndDeletion() {
foreach ($terms as $term) {
$this->assertText($term, 'The term appears on the node preview.');
}
$tree = taxonomy_get_tree($this->vocabulary->id());
$tree = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadTree($this->vocabulary->id());
$this->assertTrue(empty($tree), 'The terms are not created on preview.');
// taxonomy.module does not maintain its static caches.
......@@ -368,10 +373,12 @@ function testTermReorder() {
$this->createTerm($this->vocabulary);
$this->createTerm($this->vocabulary);
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Fetch the created terms in the default alphabetical order, i.e. term1
// precedes term2 alphabetically, and term2 precedes term3.
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->id(), 0, NULL, TRUE);
$taxonomy_storage->resetCache();
list($term1, $term2, $term3) = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);
$this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
......@@ -395,8 +402,8 @@ function testTermReorder() {
);
$this->drupalPostForm(NULL, $edit, t('Save'));
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$terms = taxonomy_get_tree($this->vocabulary->id());
$taxonomy_storage->resetCache();
$terms = $taxonomy_storage->loadTree($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.');
$this->assertEqual($terms[2]->tid, $term1->id(), 'Term 1 was moved below term 2.');
......@@ -407,8 +414,8 @@ function testTermReorder() {
// Ensure form redirected back to overview.
$this->assertUrl('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$terms = taxonomy_get_tree($this->vocabulary->id(), 0, NULL, TRUE);
$taxonomy_storage->resetCache();
$terms = $taxonomy_storage->loadTree($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.');
......@@ -438,8 +445,8 @@ function testTermMultipleParentsInterface() {
$this->assertEqual($edit['name[0][value]'], $term->getName(), 'Term name was successfully saved.');
$this->assertEqual($edit['description[0][value]'], $term->getDescription(), 'Term description was successfully saved.');
// Check that the parent tid is still there. The other parent (<root>) is
// not added by taxonomy_term_load_parents().
$parents = taxonomy_term_load_parents($term->id());
// not added by \Drupal\taxonomy\TermStorageInterface::loadParents().
$parents = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadParents($term->id());
$parent = reset($parents);
$this->assertEqual($edit['parent[]'][1], $parent->id(), 'Term parents were successfully saved.');
}
......
......@@ -148,7 +148,7 @@ function taxonomy_theme() {
* An integer that represents the level of the vocabulary's hierarchy.
*/
function taxonomy_check_vocabulary_hierarchy(VocabularyInterface $vocabulary, $changed_term) {
$tree = taxonomy_get_tree($vocabulary->id());
$tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vocabulary->id());
$hierarchy = TAXONOMY_HIERARCHY_DISABLED;
foreach ($tree as $term) {
// Update the changed term with the new parent value before comparison.
......
......@@ -97,7 +97,7 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
$replacements = array();
$sanitize = !empty($options['sanitize']);
$taxonomy_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
if ($type == 'term' && !empty($data['term'])) {
$term = $data['term'];
......@@ -133,7 +133,7 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
break;
case 'parent':
if ($parents = taxonomy_term_load_parents($term->id())) {
if ($parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents);
$replacements[$original] = SafeMarkup::checkPlain($parent->getName());
}
......@@ -146,7 +146,7 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
$replacements += $token_service->generate('vocabulary', $vocabulary_tokens, array('vocabulary' => $vocabulary), $options);
}
if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = taxonomy_term_load_parents($term->id())) {
if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents);
$replacements += $token_service->generate('term', $vocabulary_tokens, array('term' => $parent), $options);
}
......@@ -178,7 +178,7 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
break;
case 'node-count':
$replacements[$original] = \Drupal::entityManager()->getStorage('taxonomy_term')->nodeCount($vocabulary->id());
$replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
break;
}
}
......
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