$depth_count[$element->depth
]++;
} $this->
assertEquals(3,
$depth_count[0
], 'Three elements in taxonomy tree depth 0.'
);
$this->
assertEquals(2,
$depth_count[1
], 'Two elements in taxonomy tree depth 1.'
);
$this->
assertEquals(2,
$depth_count[2
], 'Two elements in taxonomy tree depth 2.'
);
$this->
assertEquals(1,
$depth_count[3
], 'One element in taxonomy tree depth 3.'
);
/** @var \Drupal\taxonomy\TermStorageInterface $storage */
$storage = \Drupal::
entityTypeManager()->
getStorage('taxonomy_term'
);
// Count parents of $term[2].
$parents =
$storage->
loadParents($term[2
]->
id());
$this->
assertCount(2,
$parents, 'The term has two parents.'
);
// Count parents of $term[3].
$parents =
$storage->
loadParents($term[3
]->
id());
$this->
assertCount(1,
$parents, 'The term has one parent.'
);
// Identify all ancestors of $term[2].
$ancestors =
$storage->
loadAllParents($term[2
]->
id());
$this->
assertCount(4,
$ancestors, 'The term has four ancestors including the term itself.'
);
// Identify all ancestors of $term[3].