In Drupal 8, sometimes we have a lot of levels of taxonomy terms and we need to manage this in our code. For example, what if we need to get a specific level of the taxonomy?
So let’s imagine the following taxonomy tree:
- Drinks
- Alcohol Drinks
- Beer
- Whiskey
- Vodka
- Alcohol Free Drinks
- Coca Cola
- Pepsi
- Milk
- Alcohol Drinks
- Fruits
- Fruits For Losing Weight
- Apricot
- Blackberrie
- Cantaloupe
- Fruits For Dogs
- Strawberry
- Watermelon
- Orange
- Fruits For Losing Weight
- Foods
- Fast Foods
- Hamburger
- Pizza
- Hot Dog
- Healthy Foods
- Almond
- Oily Fish
- Avocado
- Fast Foods
Below is a method to get only the 1st level:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 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. 1, // Get only 1st level. TRUE // Get full load of taxonomy term entity. ); $result = []; foreach ($tree as $term) { $result[] = $term->getTerm(); } dsm($result); [ 0 => 'Drinks', 1 => 'Fruits', 2 => 'Foods', ] |
Get the 1st and 2nd levels:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// 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. ); $result = []; foreach ($tree as $term) { $result[] = $term->getTerm(); } dsm($result); [ 0 => 'Drinks', 1 => 'Alcohol Drinks', 2 => 'Alcohol Free Drinks', 3 => 'Fruits', 4 => 'Fruits For Losing Weight', 5 => 'Fruits For Dogs', 6 => 'Foods', 7 => 'Fast Foods', 8 => 'Healthy Foods', ] |
Get only the 2nd level:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// 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. ); $result = []; 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()))) { $result[] = $term->getTerm(); } } dsm($result); [ 0 => 'Alcohol Drinks', 1 => 'Alcohol Free Drinks', 2 => 'Fruits For Losing Weight', 3 => 'Fruits For Dogs', 4 => 'Fast Foods', 5 => 'Healthy Foods', ] |
Get the 2nd and 3rd levels:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// 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. 3, // Get terms from 1st, 2nd and 3rd levels. TRUE // Get full load of taxonomy term entity. ); $result = []; foreach ($tree as $term) { // We get 2nd and 3rd levels, also we check parents (1st level does not have parents). if (!empty($manager->loadParents($term->id()))) { $result[] = $term->getTerm(); } } dsm($result); [ 0 => 'Alcohol Drinks', 1 => 'Bear', 2 => 'Whiskey', 3 => 'Vodka', 4 => 'Alcohol Free Drinks', 5 => 'Coca Cola', 6 => 'Pepsi', 7 => 'Milk', 8 => 'Fruits For Losing Weight', 9 => 'Apricot', 10 => 'Blackberrie', 11 => 'Cantaloupe', 12 => 'Fruits For Dogs', 13 => 'Strawberry', 14 => 'Watermelon', 15 => 'Orange', 16 => 'Fast Foods', 17 => 'Hamburger', 18 => 'Pizza', 19 => 'Hot Dog', 20 => 'Healthy Foods', 21 => 'Almond', 22 => 'Oily Fish', 23 => 'Avocado', ) |
Get only the last level:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// 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 tree. NULL, // Get all levels. TRUE // Get full load of taxonomy term entity. ); $result = []; foreach ($tree as $term) { if (empty($manager->loadChildren($term->id()))) { $result[] = $term->getTerm(); } } dsm($result); [ 1 => 'Bear', 2 => 'Whiskey', 3 => 'Vodka', 4 => 'Coca Cola', 5 => 'Pepsi', 6 => 'Milk', 7 => 'Apricots', 8 => 'Blackberrie', 9 => 'Cantaloupe', 10 => 'Strawberry', 11 => 'Watermelon', 12 => 'Orange', 13 => 'Hamburger', 14 => 'Pizza', 15 => 'Hot Dog', 16 => 'Almonds', 17 => 'Oily Fish', 18 => 'Avocado', ] |
I would like add a note about using the entity type manager service. Actually, this applies to all Drupal services… In the above examples we got this service by static method via a Drupal object:
1 2 |
// Get entity type manager service. $manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); |
But the better way to get this service is to use Dependency Injection.