You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Load the taxonomy tree with special parameters.$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 ($treeas$term) {
$results[] = $term->getTerm();
}
2- Load Taxonomy Terms first and second level
// Load the taxonomy tree with special parameters.$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
'some_vocabulary', // This is your taxonomy term vocabulary (machine name).0, // This is "tid" of parent. Set "0" to get all.2, // Get terms from 1st and 2nd levels.TRUE// Get full load of taxonomy term entity.
);
$results = [];
foreach ($treeas$term) {
$results[] = $term->getTerm();
}
3- Getting Taxonomy Terms only the second level
// Get entity type manager service.$manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
// Load the taxonomy tree with special parameters.$tree = $manager->loadTree(
'some_vocabulary', // This is your taxonomy term vocabulary (machine name).0, // This is "tid" of parent. Set "0" to get all.2, // Get terms from 1st and 2nd levels.TRUE// Get full load of taxonomy term entity.
);
$results = [];
foreach ($treeas$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();
}
}
4- Getting Taxonomy Terms only second and third level
// Get 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 ($treeas$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();
}
}
5- Getting Taxonomy Terms only the last level
// Get 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 ($treeas$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!