/**
 * Finds all children of a term ID.
 *
 * @param $tid
 *   A taxonomy term ID.
 * @param $vid
 *   An optional vocabulary ID to restrict the child search.
 *
 * @return
 *   An array of term objects that are the children of the term $tid, or an
 *   empty array when no children exist.
 */
function taxonomy_term_load_children($tid, $vid = NULL) {
  $children = &drupal_static(__FUNCTION__, array());

  if ($tid && !isset($children[$tid])) {
    $tids = \Drupal::entityManager()->getStorage('taxonomy_term')->loadChildren($tid);
    $children[$tid] = entity_load_multiple('taxonomy_term', $tids);
  }

  return isset($children[$tid]) ? $children[$tid] : array();
}

$vid is never used and is non-functional.

Comments

alexpott’s picture

Issue tags: +Novice

The fix is check that this never called with a $vid and remove it.

anthonylindsay’s picture

Status: Active » Needs review
StatusFileSize
new746 bytes

taxonomy_term_load_children is only called twice:
in
core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php
and
core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php

Patch to remove the $vid from the function definition is attached.

alexpott’s picture

Title: taxonomy_term_load_children claims to restrict by vid but this is not possible. » taxonomy_term_load_children should become a method on the Term Entity
Status: Needs review » Reviewed & tested by the community

Looking at the code it looks like this should be refactored to become a method on the Term storage but the patch attached addresses the bug.

alexpott’s picture

Title: taxonomy_term_load_children should become a method on the Term Entity » taxonomy_term_load_children claims to restrict by vid but this is not possible

Fixing title

webchick’s picture

Status: Reviewed & tested by the community » Needs review

Hm. Should we not go the other way here and fix it so limiting by $vid works? I shudder to think what this function would do no a term_data table the size of Drupal.org's, for example.

dawehner’s picture

It seems quite a special feature that you have a taxonomy term (which is a content entity with a vocab as bundle) with children in different vocabularies.
Do we actually support that at all now?

webchick’s picture

Status: Needs review » Fixed

Oops, turns out, no we don't. :D

Committed and pushed to 8.x. Thanks!

  • Commit ef2a54b on 8.x by webchick:
    Issue #2228903 by anthonylindsay | alexpott: Taxonomy_term_load_children...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.