I added two fields to my vocabulary terms field management. How do I get these values? There is no fields like color or Image in my Taxonomy Term vocabulary dump.

How does Drupal 8 store these additional fields?

$terms = $this->taxonomyStorage->loadTree($vocabularyName);



enter image description here

  • Possible duplicate of Get custom fields assigned to taxonomy – user72672 Mar 29 '18 at 16:52
  • See @GiorgosK's answer. You need to access it by its machine name which is field_color, not color – sonfd Mar 29 '18 at 23:23

Its similar to any other entity on Drupal 8

$name = $terms[0]->name;

//simple text field
$color = $terms[0]->field_color->value;

//multivalue field - get array of values
$color = $terms[0]->get('field_color')->getValue();

//entity reference field
$entity = $terms[0]->field_color->entity;

//image field 
$image = $terms[0]->get('field_image')->entity;
$image_uri = $image->getFileUri();

//get color entity id
$entity_id = $entity->id();

I think at least your color field is actually an entity reference

  • Unlucky i get error that it can't find nor field_color or color – Zbigniew Malcherczyk Mar 30 '18 at 15:56
  • the code above was tested and was working for a single TERM in api.drupal.org/api/drupal/… ... I think you better post some sample code otherwise we can't really tell why your code is not working – GiorgosK Mar 30 '18 at 16:13
  • The solution for me was: Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term->tid)->get('field_color')->color – Zbigniew Malcherczyk Mar 30 '18 at 16:13

First of all, invoke the related class

use Drupal\taxonomy\Entity\Term;

and then try this code and just replace VID with your machine

    $taxonomy_terms_vocabularies = \Drupal::entityTypeManager()
         'vid' => 'car_sub_type' 
      $taxonomy_terms_vocabularies = array_keys($taxonomy_terms_vocabularies);         
      $taxonomy_terms_vocabularies_opt = [];
      foreach($taxonomy_terms_vocabularies as $key => $val){
        $term = Term::load($val); 
        $name = $term->getName();

          $taxonomy_terms_vocabularies_opt[] = array(
            'id' => $val, 
            'name' =>  $name
            'abc' =>   $term->get('field_machine_name')->value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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