Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidjguru/3b9d36bf3e00dd338d751b7bfa2c41eb to your computer and use it in GitHub Desktop.
Save davidjguru/3b9d36bf3e00dd338d751b7bfa2c41eb to your computer and use it in GitHub Desktop.
Drupal 8 || 9 - Getting Taxonomy Terms from different levels programmatically

1- Load Taxonomy Terms only first level

// 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 ($tree as $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 ($tree as $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 ($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();
  }
}

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 ($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();
  }
}

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 ($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();
  }
}
@mialdi98
Copy link

Thanks, it was really helpful!

@davidjguru
Copy link
Author

@mialdi98 Thanks for the feedback! Very much appreciated.

@sebastianzapateiro
Copy link

Thanks a lot!

@kamiloCervantes
Copy link

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment