1

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);

//CAN ACCESS
$terms[0]->name

//CAN'T ACCESS
$terms[0]->color

enter image description here

2
  • Possible duplicate of Get custom fields assigned to taxonomy
    – user72672
    Mar 29, 2018 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, 2018 at 23:23

3 Answers 3

3

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

3
  • Unlucky i get error that it can't find nor field_color or color Mar 30, 2018 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, 2018 at 16:13
  • The solution for me was: Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term->tid)->get('field_color')->color Mar 30, 2018 at 16:13
0

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()
         ->getStorage('taxonomy_term')
         ->loadByProperties([ 
         '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
          );      
      }
0

if the taxonomy is a entity reference field :

 $current_entity_lang = $entity->get('langcode')->value;
    //version and so current revision
    $translated_entity = $entity->getTranslation($current_entity_lang);
    // if single value for the taxo
    $value=$translated_entity->get('field_name')->referencedEntities()[0]->getName();
    //else loop the values to get name
    $values=$translated_entity->get('field_name')->referencedEntities();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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