3

I want load all the taxonomy terms by their vocabulary ID in Drupal 8. In Drupal 7, I would use the following code.

$terms = taxonomy_get_tree($vid);

What code should I use in Drupal 8?

2

May this could hep you.

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid,0,NULL,TRUE);
print_r($terms);
|improve this answer|||||
7

You can load terms using the entity query,

$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($terms as $term) {
     $term_data[] = array(
      "id" => $term->tid,
      "name" => $term->name
     );
    }
|improve this answer|||||
  • Thanks for quick reply.I used that and when I dpm($terms) result show like this. – Pankaj Yogi Oct 12 '16 at 9:16
  • Array ( [0] => stdClass Object ( [CLASS] => stdClass ) [1] => stdClass Object ( [CLASS] => stdClass ) [2] => stdClass Object ( [CLASS] => stdClass ) ) – Pankaj Yogi Oct 12 '16 at 9:17
  • You can loop through the array to get term id and name. Updated the answer. – Syam Oct 12 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.