How to get fields from taxonomy terms?

by Neil Hillman   Last Updated September 11, 2019 16:07 PM - source

I am able to retrieve taxonomy terms programmatically, (as long as I only want to reference the default fields, 'tid' and 'name'). But how do I access custom taxonomy fields?

$vid = 'MACHINE_NAME';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($terms as $term) {
    $tid = $term->tid;
    $term_name = $term->name;

    // REFERENCE CUSTOM FIELD VALUES HERE...

}

I have tried all manor of ways I have seen, but can't get any of them working, eg:

$value = $term->FIELD_NAME->value;
$value = $term->get('FIELD_NAME')->getValue();

How do I reference custom taxonomy term fields by their machine name, from what is returned by getStorage('taxonomy_term')->loadTree($vid)?



Related Questions



Drupal 7 user access to the taxonomy

Updated November 09, 2017 22:07 PM

How to get taxonomy term name with url by tid

Updated May 03, 2018 08:07 AM