taxonomy_get_tree() returns all the terms in an array, but you have no way to find the data for a given term inside that array without iterating through the whole thing. This is silly (I'm tempted to call this a bug). It's been bothering me for a while, and I've had to work-around it by wastefully iterating through the tree once to make a new array indexed by tid, which I can then actually use. We had a very similar problem with node_revision_list() over at #114703: minor node revisions fixes.

Here's a patch for D7's taxonomy.module. I can't see how there'd be objections to this.

However, meanwhile, I'd like to see if there's any chance this will be committed to D6 and/or D5. This patch applies (with minor offsets) to D6, and would be trivial to backport to D5 if that'd fly. Given the outcome of #114703, I doubt this will go into D5, but maybe D6? ;)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers’s picture

This has bothered me too for a long time. This, among other things, is necessary to clean up the Taxonomy module.

catch’s picture

Status: Needs review » Needs work

This is a great idea, but it breaks drag and drop in taxonomy administration.

    * notice: Undefined offset: 0 in /home/catch/www/7/modules/taxonomy/taxonomy.admin.inc on line 463.
    * notice: Undefined index: tid in /home/catch/www/7/modules/taxonomy/taxonomy.admin.inc on line 464.
    * notice: Undefined index: parents in /home/catch/www/7/modules/taxonomy/taxonomy.admin.inc on line 465.
    * notice: Undefined index: weight in /home/catch/www/7/modules/taxonomy/taxonomy.admin.inc on line 465.
    * notice: Undefined index: parents in /home/catch/www/7/modules/taxonomy/taxonomy.admin.inc on line 471.
eojthebrave’s picture

Was playing patch bingo tonight and came across this one and agree that it would be a good improvement.

This should fix the problem.

Steps to test.

1. Navigate to the "list terms" page for a Vocabulary that has terms.
2. Reorder the terms.
3. Save

catch’s picture

Status: Needs work » Needs review
FileSize
3.7 KB

Patch was reversed, here's a re-roll which should apply cleanly.

We need at least a basic test for taxonomy_get_tree really. I don't think the terms overview page is tested either.

cburschka’s picture

1.) Applies
2.) Patched site installs.
3.) Created basic tag hierarchy
4.) Shuffled around, works.
5.) Saved with the new order, works.

I encountered a nasty PDO error while creating one of the terms, but that's probably unrelated and I'll file a separate issue for it.

Specifically, when creating a term and selecting the <root> item as a parent (which should be the same as selecting nothing at all):

PDOException: INSERT INTO {term_hierarchy} (tid, parent) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1) - Array ( [:db_insert_placeholder_0] => 4 [:db_insert_placeholder_1] => ) SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'parent' at row 1 in taxonomy_term_save() (line 359 of modules/taxonomy/taxonomy.module).
Wim Leers’s picture

[:db_insert_placeholder_1] should equal 0. So that's probably an error in the #submit callback.

catch’s picture

damn, the term objects page also converted a bunch of queries to dbtng, and that doesn't do things like silently case empty strings to 0/NULL for you or whatever this code was relying on. Patch and test posted at #332145: UNSTABLE-3 blocker: taxonomy_form_term_submit passes empty string as parent [dbtng conversion regression]

Status: Needs review » Needs work

The last submitted patch failed testing.

lilou’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch failed testing.

eojthebrave’s picture

re-rolled against current head. Passes current set of taxonomy tests.

eojthebrave’s picture

Status: Needs work » Needs review

Marking as needs review

catch’s picture

Note that taxonomy_get_tree gets a bit more coverage than it does already in #144969: taxonomy_term_count_nodes returns wrong count (+ tests). I'll see about adding an explicit test to this issue later on though if no-one beats me to it.

Status: Needs review » Needs work

The last submitted patch failed testing.

eojthebrave’s picture

Status: Needs work » Needs review
FileSize
1.97 KB

bugger. Still learning how to use git, looks like my patch didn't actually apply. Should work now.

catch’s picture

Status: Needs review » Needs work

We can't do this. taxonomy_get_tree() returns the entire taxonomy tree - in which a term might appear more than once when multiple parents are enabled, with different depth for example.

However, in a lot of cases you don't need all that information, so we might consider something like taxonomy_get_descendants() which indexed by tid, or indexing by tid for vocabularies without multiple parents inside taxonomy_get_tree() itself.

Xano’s picture

Also, indexing terms means we lose the order in which we fetch them from the DB.

dww’s picture

Title: array returned by taxonomy_get_tree() should be indexed by tid » Add taxonomy API call to fetch terms in a vocabulary indexed by tid
Assigned: dww » Unassigned
Category: task » feature
Status: Needs work » Active

@catch #16: "We can't do this. taxonomy_get_tree() returns the entire taxonomy tree - in which a term might appear more than once when multiple parents are enabled, with different depth for example."

Argh. :( What a pain.

@xano #17: "indexing terms means we lose the order in which we fetch them from the DB"

Ugh.

SELECT t.tid, t.*, parent FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name

We lose that ORDER BY. I suppose there are cases where you need that.

Guess this is a feature request for a brand new API function that gives you all terms in a vocab indexed by tid or something.

catch’s picture

Title: Add taxonomy API call to fetch terms in a vocabulary indexed by tid » Add taxonomy_get_descendents()
Status: Active » Needs work

You mean like taxonomy_term_load_multiple(array(), array('vid' => $vid)) ?

Having said that though, we could definitely add an API function like taxonomy_get_descendents() returning an array of all children indexed by tid - since we just want the terms in that case, not a representation of their depth in the hierarchy. So leaving this open.

Xano’s picture

Moments after my last reply I realised that although the query used by taxonomy_get_tree() sorts terms by weight and name, this order gets lost later on in the function. We might want to figure out a way to make sure this order remains, so contrib can use Taxonomy's function to display terms in the same order at all times.

Xano’s picture

catch’s picture

Version: 7.x-dev » 8.x-dev

Between taxonomy_get_tree() and taxonomy_load_multiple() we have most of this, I don't think this'll get done in the next day, so moving to D8

xjm’s picture

Jody Lynn’s picture

Status: Needs work » Closed (duplicate)
Pancho’s picture

Title: Add taxonomy_get_descendents() » Add taxonomy_term_load_descendents()
Status: Closed (duplicate) » Needs work

Re #24:
Nope, taxonomy_term_load_children() has existed before and is not the same as taxonomy_term_load_descendents().
Therefore reopening.

Also, adapting title after the #1377628-9: taxonomy_get_term_by_name() should be taxonomy_term_load_multiple_by_name() rename.

Pancho’s picture

mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.