11

This question already has an answer here:

I want to load taxonomy terms by tid, but I didn't found how. Only how to load taxonomy by name, but now I have term id and I need to get value of fields.

I call this on public function build() for custom block.

My code:

$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('menu');
foreach ($terms as $term) {
    //var_dump($term->tid);
}

marked as duplicate by kenorb, mradcliffe, DRUPWAY, Neograph734, Shawn Conn Jun 20 '17 at 16:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Check taxonomy_term_load_multiple – Jimmy Ko Jul 3 '16 at 22:12
  • Thx i use $entityManager->getStorage('taxonomy_term')->loadMultiple($terms);. But now all field (preperties) are private :'( – Kakoum Jul 3 '16 at 23:47
  • Yes, but you can get their values with the term methods, for example $name = $term->getName(); Look into the TermInterface to see which methods are available. – Maouna Jul 4 '16 at 15:19
13

in Single Manner

    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(TID);
    $value = $term->FIELD_MACHINENAME->value;

Example : load title and language of term id =2

    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(2);
    $title = $term->name->value;
    langcode = $term->langcode->value;

And for load Multiple as you wanted

  $terms = $entityManager->getStorage('taxonomy_term')->loadMultiple($t‌​erms);
  foreach ($terms as $term) {
      var_dump($term->tid->value); //return tid of term
   var_dump($term->name->value); //return title of term
  }
  • loadMultiple($t‌​erms)... what is $terms – AlxVallejo Jan 24 '18 at 19:17
7

You can also do like this:

$tids = [1,2,3];
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);

Example from : http://www.drupal8.ovh/en/tutoriels/17/get-taxonomy-terms-of-a-vocabulary-drupal-8

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