For all the next cases we will use the loadTree() method, from the TermStorage class in Drupal. This method finds all terms in a given vocabulary ID, using the next parameters:
- string $vid: Vocabulary ID to retrieve terms for.
- int $parent: The term ID under which to generate the tree. If 0, generate the tree for the entire vocabulary.
- int $max_depth: The number of levels of the tree to return. Leave NULL to return all levels.
- bool $load_entities: If TRUE, a full entity load will occur on the term objects. Otherwise they are partial objects queried directly from the {taxonomy_term_data} table to save execution time and memory consumption when listing large numbers of terms. Defaults to FALSE.
And this method will return an array of terms objects that are the children of the vocabulary $vid. We're gonna to play extracting terms and then processing the values cutting some levels (up or down) in order to get only the terms from the desired levels.
// Load the taxonomy tree using values.
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
'vocabulary_name', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all.
1, // Get only 1st level.
TRUE // Get full load of taxonomy term entity.
);
$results = [];
foreach ($tree as $term) {
$results[] = $term->getTerm();
}
// Load the taxonomy tree using values.
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
'some_vocabulary', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all.
2, // Get terms from 1st and 2nd levels.
TRUE // Get full load of taxonomy term entity.
);
$results = [];
foreach ($tree as $term) {
$results[] = $term->getTerm();
}
// Load the taxonomy tree using values.
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
'some_vocabulary', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all.
2, // Get terms from 1st and 2nd levels.
TRUE // Get full load of taxonomy term entity.
);
$results = [];
foreach ($tree as $term) {
// We get 1st and 2nd levels, also we check parents (only 2nd level has parents).
if (!empty($manager->loadParents($term->id()))) {
$results[] = $term->getTerm();
}
}
// Getting first the entity type manager service.
$manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
// Load the taxonomy tree using values.
$tree = $manager->loadTree(
'some_vocabulary', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all.
3, // Get terms from 1st, 2nd and 3rd levels.
TRUE // Get full load of taxonomy term entity.
);
$result = [];
foreach ($tree as $term) {
// We want only 2nd and 3rd levels so we check parents cause 1st level does not have parents.
if (!empty($manager->loadParents($term->id()))) {
$result[] = $term->getTerm();
}
}
// Getting first the entity type manager service.
$manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
// Load the taxonomy tree using values.
$tree = $manager->loadTree(
'some_vocabulary', // The taxonomy term vocabulary machine name.
0, // The "tid" of parent using "0" to get all tree.
NULL, // Get all levels.
TRUE // Get full load of taxonomy term entity.
);
$result = [];
foreach ($tree as $term) {
// Check if has children or not to validate if is at last level.
if (empty($manager->loadChildren($term->id()))) {
$result[] = $term->getTerm();
}
}
Thanks, it was really helpful!