Wednesday, August 21, 2019

access taxonomy fields programatically in drupal 8

Access one term:    
    $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

  $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
  }

No comments:

Post a Comment