Refactor taxonomy hierarchy API for performance, consistency, and convenience

Created on 2 July 2011, almost 14 years ago
Updated 6 May 2025, 6 days ago

Currently, the available API for selecting taxonomy hierarchies is very inconsistent. For example:

  1. taxonomy_get_tree() makes the loading of term entities optional, but seemingly related functions -- taxononomy_get_children(), taxonomy_get_parents(), taxonomy_get_parents_all(), and taxonomy_get_parents_all() -- all force the entities to be loaded.

  2. taxonomy_get_tree() has data about the direct children of each term, but does not return it. So, callers have to choose between rearranging the array to find the children of each term based on what claims it as a parent, or using taxonomy_get_children(), which only allows the fetching of one term at a time and forces the loading of term entities.

  3. taxonomy_get_parents_all() is provided, but no taxonomy_get_children_all() (or taxonomy_get_descendents() as in #251595: Add taxonomy_term_load_descendents() )

  4. taxonomy_get_parents_all() in particular runs a lot of queries in addition to loading all the parent entities. It could probably be refactored to improve performance.

  5. It would be better to have taxonomy_get_parents_multiple() and taxonomy_get_children_multiple() to reduce the number of queries needed, and for the existing functions to be single-value wrappers of these. (One is a special case of many.)

  6. If #5 above were implemented, it might be best to rename taxonomy_get_parents_all() to taxonomy_get_ancestors() for clarity.

A refactor might potentially leverage some of the flexibility in the new database API, perhaps with a single base taxonomy hierarchy query that is modified to fit the particular case. (It might also be very useful to allow callers add contextual query tags to the queries.)

API functions:

Possibly relevant issues (some are old and parts may no longer be applicable since #556842: taxonomy_get_tree() memory issues went into 7.x):

📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component

taxonomy.module

Created by

🇺🇸United States xjm

Live updates comments and jobs are added and updated live.
  • API change

    Changes an existing API or subsystem. Not backportable to earlier major versions, unless absolutely required to fix a critical bug.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024