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

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

  • 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

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.