Apologies if this has already been fixed somewhere. did look through the issue queue and couldn't find anything.
(Sorry, I'm also not currently geared up for producing patches).

The following code starting at line 858 of modules/taxonomy/taxonomy.module in my Drupal 7.0 code is:

function taxonomy_get_children($tid, $vid = 0) {
  $children = &drupal_static(__FUNCTION__, array());

  if ($tid && !isset($children[$tid])) {

When $tid=0 this always results in no child terms being returned. Code should be something like:

function taxonomy_get_children($tid, $vid = 0) {
  $children = &drupal_static(__FUNCTION__, array());

  if (isset($tid) && !isset($children[$tid])) {

Comments

damien tournoud’s picture

Priority: Major » Normal
Status: Needs review » Active

On the other hand, there are no term with tid = 0, lowering priority. It makes sense to fix this.

simg’s picture

Priority: Normal » Major

sorry, mis-understanding.
top level terms have a parent tid of 0 - these should be returned but are not.
(ie the tid parameter passed to get_children is the parent tid)
will assume this raises priority ?

damien tournoud’s picture

A parent tid of 0 means that there is no parent [it should rather be NULL, but that's the way it currently works].

simg’s picture

based on the values in the table taxonomy_term_hierarchy a parent tid of 0 means a top level term ? a value of NULL would mean an unknown parent, which wouldn't make much sense ?

with the current taxonomy_get_children code, there is no way to select top level terms. (There is currently no way to do this using any taxonomy API call).

This example code http://drupal.org/node/403348 doesn't work because a call to taxonomy_get_children(0, $vid) always returns an empty array.

bfroehle’s picture

Maybe taxonomy_get_tree($vid, 0, 1); ?

simg’s picture

I have a project coming up that will need to handle 65,000 taxonomy nodes or thereabouts. i don't think taxonomy_get_tree will do much for scalability if I just need to get top level terms

bfroehle’s picture

One parameter to taxonomy get tree is the number of levels you want to retrieve...

simg’s picture

ok, thanks bfroehle :)

nonetheless, it would be nice to fix this bug in taxonomy_get_children ...

bfroehle’s picture

Well it should be pretty easy to make a patch for it. See http://drupal.org/patch/create

catch’s picture

Category: bug » task
Priority: Major » Normal

This isn't a major bug, just a nice to have. I'm not really convinced that 0 should be treated as a valid tid but it won't necessarily make anything worse.

simg’s picture

sorry, I wasn't sure if it qualified as major or not, but neither is it a "nice to have". this is a bit of quite important functionality that does not work as documented and is likely to cause quite a bit of stress and lost time (as it did for me).

the way the taxonomy data structure is constructed, 0 is not a valid tid but it is a valid *parent tid*.

I'm not very familiar with git and certainly not set up for using to create core patches (no doubt I will be at some point). I was assuming this would be quick and obvious for someone to just cut/paste ?

bartk’s picture

Subscribing.

When trying to traverse a taxonomy tree recursively, it would be nice not to have to call a different function to get the top level terms.

mr.baileys’s picture

Version: 7.x-dev » 8.x-dev
Status: Active » Needs review
StatusFileSize
new1.25 KB

Patch attached. Using isset() as proposed in the OP doesn't really make sense since $tid is a mandatory function argument and thus always set.

simg’s picture

Thanks Mr Baileys, your patch seems like a better approach :)

jcisio’s picture

fietserwin’s picture

Category: task » feature
Status: Needs review » Needs work

@jcisio: thanks for rerolling my patch from the other issue to D8. My patch also works when the top level terms are queried for different vid's: the static cache will distinguish these cases and store them separately...

- I think we should integrate the documentation change from #13.
- Some tests should be welcome.

@BartK (#12): +1
@Catch (#10): What I don't get is why there was/is a parameter $vid at all, if $tid = 0 would not be allowed. Because tids are already unique and thus unambiguously define the vid, unless tid = 0.

jibran’s picture

Category: Feature request » Task
Issue summary: View changes
Status: Needs work » Active

taxonomy_get_children got renamed to taxonomy_term_load_children in #1377628: taxonomy_get_term_by_name() should be taxonomy_term_load_multiple_by_name(). And taxonomy_term_load_children is deprecated in #1976298: Move taxonomy_get_tree() and associated functions to Taxonomy storage, deprecate procedural wrappers. but the existing code includes the fix from #15

  /**
   * {@inheritdoc}
   */
  public function loadChildren($tid, $vid = NULL) {
    if (!isset($this->children[$tid])) {
      $children = array();
      $query = $this->database->select('taxonomy_term_field_data', 't');
      $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
      $query->addField('t', 'tid');
      $query->condition('h.parent', $tid);
      if ($vid) {
        $query->condition('t.vid', $vid);
      }
      $query->condition('t.default_langcode', 1);
      $query->addTag('term_access');
      $query->orderBy('t.weight');
      $query->orderBy('t.name');
      if ($ids = $query->execute()->fetchCol()) {
        $children = $this->loadMultiple($ids);
      }
      $this->children[$tid] = $children;
    }
    return $this->children[$tid];
  }

But this issue is still valid for D7 so moving it to 7.x. Because it is fixed in 8.x it's a task in 7.x

jibran’s picture

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

doh!!

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.