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
Comment #1
damien tournoud commentedOn the other hand, there are no term with tid = 0, lowering priority. It makes sense to fix this.
Comment #2
simg commentedsorry, 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 ?
Comment #3
damien tournoud commentedA parent tid of 0 means that there is no parent [it should rather be NULL, but that's the way it currently works].
Comment #4
simg commentedbased 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.
Comment #5
bfroehle commentedMaybe
taxonomy_get_tree($vid, 0, 1);?Comment #6
simg commentedI 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
Comment #7
bfroehle commentedOne parameter to taxonomy get tree is the number of levels you want to retrieve...
Comment #8
simg commentedok, thanks bfroehle :)
nonetheless, it would be nice to fix this bug in taxonomy_get_children ...
Comment #9
bfroehle commentedWell it should be pretty easy to make a patch for it. See http://drupal.org/patch/create
Comment #10
catchThis 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.
Comment #11
simg commentedsorry, 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 ?
Comment #12
bartk commentedSubscribing.
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.
Comment #13
mr.baileysPatch attached. Using isset() as proposed in the OP doesn't really make sense since $tid is a mandatory function argument and thus always set.
Comment #14
simg commentedThanks Mr Baileys, your patch seems like a better approach :)
Comment #15
jcisio commentedHere is a reroll from #1136408: taxonomy_get_children should support getting top level terms.
Comment #16
fietserwin@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.
Comment #17
jibrantaxonomy_get_childrengot renamed totaxonomy_term_load_childrenin #1377628: taxonomy_get_term_by_name() should be taxonomy_term_load_multiple_by_name(). Andtaxonomy_term_load_childrenis deprecated in #1976298: Move taxonomy_get_tree() and associated functions to Taxonomy storage, deprecate procedural wrappers. but the existing code includes the fix from #15But 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
Comment #18
jibrandoh!!