call the taxonomy class.
use Drupal\taxonomy\Entity\Term;
To get the child terms using parent term id,
$parent_id = 'Parent Term Id';
$vocabulary = 'Taxonomy name';
function getChildren(string $parent_id): array {
$child_terms = \Drupal::entityTypeManager->getStorage('taxonomy_term')->loadTree(
$vocabulary,
$parent_id,
NULL,
FALSE
);
$children = [];
if(empty($child_terms)) {
//This is a parent tid with no child terms.
return $parent_id;
}
foreach ($child_terms as $term) {
$children[] = $term->tid;
}
return $children;
}
- Log in to post comments
Category