Get taxonomy terms of a vocabulary - Drupal 8

Submitted by editor on Tue, 11/17/2015 - 19:13
Question

How to get the taxonomy terms  list of a selectes vocabulary ?

Using entityQuery, you can get the taxonomy terms  of a particular vocabulary.

    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', "tags");
    $tids = $query->execute();
    $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);

If you want to get several vocabulary, use OR condition like that.
    $query_or->condition('vid', "tags");
    $query_or->condition('vid', "test");
    $query->condition($query_or);

Get Taxonomy name
      $name = $term->toLink()->getText();

Create link to the term
      $link = \Drupal::l($term->toLink()->getText(), $term->toUrl());

NOTE : I don't know it is the best way, but it's working

 

Comments

slivorezka (not verified)

Mon, 11/07/2016 - 10:10

    $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('tags', $parent = 0, $max_depth = NULL, $load_entities = FALSE);

$terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('nieuws_category', $parent = 0, $max_depth = NULL, $load_entities = FALSE);

gaurav (not verified)

Thu, 01/12/2017 - 16:45

I have got the list of terms but i want to print these terms in my views block  twig file.
so how can i do that ?

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.