Status:
A partial fix has been committed to D7 in #346156-14: term delete fails because of misnamed function.
However, per #13 and a duplicate issue against D7 (see #19) this still doesn't work correctly.
A new patch against D7 is available in #16

#1347542: [META] Taxonomy API improvements
#251595: Add taxonomy_term_load_descendents()
#346156: term delete fails because of misnamed function
#394422: Taxonomy terms should be listed in order they are entered
#1207326: Refactor taxonomy hierarchy API for performance, consistency, and convenience

Original report by Pancho

Suppose you have a multiple hierarchy vocabulary with the following terms:

Politics (tid=1)
-- Event (tid=2)
Sports (tid=3)
-- Tennis (tid=4)
---- Event (tid=2)

Calling taxonomy_get_parents_all(2), you would expect an array with all ancestors of the Event term.
Instead, the array contains (in this order) only the terms Event, Politics and Tennis, while Sports is missing.

If you add a positive weight to Politics, you receive the following vocabulary:

Sports (tid=3)
-- Tennis (tid=4)
---- Event (tid=2)
Politics (tid=1)
-- Event (tid=2)

Calling taxonomy_get_parents_all(2) now gives you the expected array with all ancestors of the Event term: Event, Tennis, Politics and Sports.

So the search for ancestors starts with the branch that has the least weight and then bounces between the branches, searching for another generation of ancestors. But as soon as any ancestor has no more parents, the complete search is stopped.
Instead it would be correct to keep on analyzing the other ancestors if they have more parents.
In the first example: Politics has no more parents, so the algorithm still spits out Tennis, but stops then. Correct would be to abandon the Politics branch, but keep on crawling the Tennis branch. Then we would have found Sports as well.

I also think it would make a lot of sense to add parents to the output array. Maybe we also want some control over how many parent generations are displayed. But these are nice features for D7, not the D6 bug itself.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cburschka’s picture

Status: Active » Needs review
FileSize
800 bytes

Very straightforward. Patch is attached below.

Example vocabulary was created according to the original post. This command was executed pre- and post-patch:

var_dump(taxonomy_get_parents_all(14));

Pre-patch:

array(3) {
  [0]=>
  object(stdClass)#28 (5) {
    ["tid"]=>
    string(2) "14"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(5) "Event"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
  [1]=>
  object(stdClass)#27 (5) {
    ["tid"]=>
    string(2) "11"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(8) "Politics"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
  [2]=>
  object(stdClass)#26 (5) {
    ["tid"]=>
    string(2) "13"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(6) "Tennis"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
}

Post-patch:

array(4) {
  [0]=>
  object(stdClass)#23 (5) {
    ["tid"]=>
    string(2) "14"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(5) "Event"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
  [1]=>
  object(stdClass)#24 (5) {
    ["tid"]=>
    string(2) "11"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(8) "Politics"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
  [2]=>
  object(stdClass)#25 (5) {
    ["tid"]=>
    string(2) "13"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(6) "Tennis"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
  [3]=>
  object(stdClass)#26 (5) {
    ["tid"]=>
    string(2) "12"
    ["vid"]=>
    string(1) "3"
    ["name"]=>
    string(6) "Sports"
    ["description"]=>
    string(0) ""
    ["weight"]=>
    string(1) "0"
  }
}

As you said in the other issue, taxonomy breadcrumbs need to work around the multiple hierarchies. This patch doesn't fix that, it only deals with the bug above.

Wim Leers’s picture

Subscribing. This would need to be backported to Drupal 5 as well.

toemaz’s picture

Subscribing. This bug appeared to me when using the hierarchical_select module.

Pancho’s picture

Version: 6.x-dev » 7.x-dev
Priority: Normal » Critical

I'd call this a critical bug.
It needs to be fixed in D7 first, then backported to D6 and D5.
Still applies to HEAD. Please test.

pp’s picture

I have a new copy of 7.x-dev. I used the patch. It worked good. Example vocabulary was created according to the original post. I saw what Arancaytar saw.

SimpleTest taxonomy modul:
62 passes, 0 fails, 0 exceptions

pp

catch’s picture

This still applies and looks good, but IMO we need a test for it.

catch’s picture

Rolled in an E_ALL fix from #351669: taxonomy_get_parents_all() is not E_ALL compliant which also needs backporting to Drupal 6. Also added a test which covers both bugs..

catch’s picture

Status: Needs review » Needs work

Here it is without an infinite loop, but in this case the (very basic) test fails.
I'm more interested in getting #344019: New implementation for database tree parsing ( taxonomy / comment ) in than trying to fix these recursive queries.

catch’s picture

catch’s picture

Priority: Critical » Normal

Split the tests off to #352479: Tests for taxonomy_get_parents_all()

Also demoting from critical.

Wim Leers’s picture

.

Summit’s picture

Hi,

This patch is not working correct. I need it to get breadcrumbs correct for weblinks on D6: http://drupal.org/node/735874#comment-2690350

I tried the latest patch and got WSOD.
I added the D7 functions taxonomy_term_load and taxonomy_term_load_multiple, but still WSOD.

Edit: Will implement this module: http://drupal.org/project/taxidermy help?

Thanks a lot in advance for working on this!
greetings,
Martijn

Stevel’s picture

+++ modules/taxonomy/taxonomy.module	29 Dec 2008 13:33:51 -0000
@@ -767,11 +767,13 @@ function taxonomy_get_parents($tid, $key
-  if ($tid) {
-    $parents[] = taxonomy_term_load($tid);
+  if ($term = taxonomy_term_load($tid)) {
+    $parents[] = $term;

This part got committed in #346156: term delete fails because of misnamed function, so this function might actually be working now. Lets try out the tests from #352479: Tests for taxonomy_get_parents_all().

Stevel’s picture

Status: Needs work » Needs review
FileSize
2.24 KB

Rolled a new patch with a different way of searching. Also included the test as it is the only way of telling whether the function actually works correct.

Stevel’s picture

Last patch wasn't correct, changed some details.

Pancho’s picture

Title: taxonomy_get_parents_all() doesn't work correctly with multiple hierarchy terms » taxonomy_term_load_parents_all() doesn't work correctly with multiple hierarchy terms
Version: 7.x-dev » 8.x-dev
Priority: Normal » Major
Status: Needs review » Needs work
Issue tags: +Needs backport to D7

Still applies for D8. The function is just called taxonomy_term_load_parents_all() now.

Pancho’s picture

Issue tags: +Needs backport to D6

Also closed #873414: taxonomy_get_parents_all() doesn't work correctly with multiple hierarchy terms as an exact duplicate. Note that this issue contains a patch against D6.

Pancho’s picture

Closed #1982032: taxonomy_get_parents_all doesn't work as (I) expected as yet another duplicate of this. It contains a patch against D7 that basically resembles these one here.

Pancho’s picture

Issue summary: View changes

Update current status

Pancho’s picture

Ported the patch in #16 to D8.

Also added Unit Tests for both taxonomy_term_load_parents() and taxonomy_term_load_parents_all(), which currently aren't tested at all.
Test results are as expected: taxonomy_term_load_parents() passes the tests, while taxonomy_term_load_parents_all() only does so with the patch applied.

From test duration, performance should remain the same.

Status: Needs review » Needs work

The last submitted patch, taxonomy_load_parents_all-217676-20-tests_only.patch, failed testing.

Pancho’s picture

Status: Needs work » Needs review

Tests-only patch correctly failed, so this is ready for review.

Pancho’s picture

Issue summary: View changes

mention D7 patch, add link to meta issue

mr.baileys’s picture

Status: Needs review » Needs work

I confirmed that the bug is still present in 8.x HEAD, and that this patch solves the issue.

The code looks sound to me, just two small remarks:

  • A re-roll is needed, since the most recent patch no longer applies cleanly to HEAD
  • No space between ! and isset().
    +++ b/core/modules/taxonomy/taxonomy.module
    @@ -565,11 +565,18 @@ function taxonomy_term_load_parents_all($tid) {
    +          if (! isset($parents[$new_parent->id()])) {
    
mr.baileys’s picture

  • Re-rolled to keep up with HEAD
  • Removed the space between ! and isset()
  • Added an additional test.

Status: Needs review » Needs work

The last submitted patch, 24: 217676-24-taxonomy-load-all-ancestors.patch, failed testing.

mr.baileys’s picture

Status: Needs work » Needs review
mr.baileys’s picture

The same test-only patch that fails in #20 is now passing in #24. Since all terms share the same weight, the order in which parents for a given term are returned is defined by the term names (see TermStorageController::loadParents()). Since weight of terms plays a role in whether or the result from taxonomy_term_load_parents_all() is correct, this causes the tests to fail in only half of the test runs.

Attached is a new test-only patch, which now forces weight on $term[1] to trigger the bug, and a new complete patch (fix + tests)

Status: Needs review » Needs work

The last submitted patch, 27: 217676-27-taxonomy-load-all-ancestors.patch, failed testing.

mr.baileys’s picture

Status: Needs work » Needs review

The last submitted patch, 27: 217676-27-taxonomy-load-all-ancestors-tests-only.patch, failed testing.

The last submitted patch, 27: 217676-27-taxonomy-load-all-ancestors-tests-only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 27: 217676-27-taxonomy-load-all-ancestors.patch, failed testing.

mgifford’s picture

Seems like this function taxonomy_term_load_parents_all() has been rewritten

function taxonomy_term_load_parents_all($tid) {
  return \Drupal::entityManager()->getStorage('taxonomy_term')->loadAllParents($tid);
}

Pointing everything to core/modules/taxonomy/src/TermStorage.php and public function loadAllParents($tid) {

This will need to be re-rolled with someone understanding TermStorage.php.

andypost’s picture

Status: Needs work » Needs review
FileSize
2.85 KB

proper re-roll, still does not get how to work with "cycled" terms

jibran’s picture

+++ b/core/modules/taxonomy/src/Tests/TermUnitTest.php
@@ -58,6 +58,12 @@ function testTaxonomyVocabularyTree() {
+    // taxonomy_term_load_parents_all().

@@ -98,5 +104,21 @@ function testTaxonomyVocabularyTree() {
+    $parents = taxonomy_term_load_parents($term[3]->id());
...
+    $ancestors = taxonomy_term_load_parents_all($term[2]->id());
...
+    $ancestors = taxonomy_term_load_parents_all($term[3]->id());

We can update this.

andypost’s picture

FileSize
1.96 KB

interdiff

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thanks

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 37: 217676-37.patch, failed testing.

andypost queued 37: 217676-37.patch for re-testing.

larowlan’s picture

+1 thanks

jibran’s picture

Status: Needs work » Reviewed & tested by the community

Test bot fluke.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed 99876e6 and pushed to 8.0.x. Thanks!

  • alexpott committed 99876e6 on 8.0.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...
andypost’s picture

Version: 8.0.x-dev » 7.x-dev
ckng’s picture

Status: Patch (to be ported) » Needs review
FileSize
1.92 KB

This is a reroll of #26 for D7.

  • alexpott committed 99876e6 on 8.1.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...

  • alexpott committed 99876e6 on 8.3.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...

  • alexpott committed 99876e6 on 8.3.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...
Sagar Ramgade’s picture

Any update when this is going to be committed to 7.x?

mgifford’s picture

Re-posting patch from 2 years ago to trigger the bot.

  • alexpott committed 99876e6 on 8.4.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...

  • alexpott committed 99876e6 on 8.4.x
    Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka...