Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Get taxonomy terms of a vocabulary - Drupal 8

By editor , 17 November, 2015
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

 

Tags

  • Drupal 8
  • Code
  • Taxonomy
  • Add new comment

Comments7

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Anonymous (not verified)

10 years 11 months ago

To sort by weight, we can…

To sort by weight, we can add

$query->sort('weight');

  • Reply

slivorezka (not verified)

10 years ago

$tree = \Drupal:…

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

  • Reply

gaurav (not verified)

9 years 10 months ago

In reply to $tree = \Drupal:… by slivorezka (not verified)

Get Terms by taxonomy

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

  • Reply

Farhan (not verified)

9 years 9 months ago

In reply to $tree = \Drupal:… by slivorezka (not verified)

How can we load terms from…

How can we load terms from multiple vocabulary using this storage.

  • Reply

gaurav (not verified)

9 years 10 months ago

print terms in twig file.

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 ?

 

  • Reply

MichaëlSoe (not verified)

9 years 2 months ago

In reply to print terms in twig file. by gaurav (not verified)

Using a viewbuilder.

\Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple($loaded_terms, 'taxonomy_teaser');

  • Reply

k_a_l (not verified)

8 years 8 months ago

Get name & tid from loaded terms

foreach ($terms as $term) {
    $name = $term->get('name')->getString();
    $tid = $term->get('tid')->getString();
}

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer