Question

I needed the field list related a vocabulary item.I was able to list the fields related to a content type by using like this:

\Drupal::entityManager()->getFieldDefinitions('node', 'article');

But I can't find a way for the vocabulary items in Drupal 8 or 9

Was it helpful?

Solution

\Drupal::entityManager() is deprecated, instead you should use \Drupal::entityTypeManager. From the docs:

Deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service.

This is especially relevant here as the getFieldDefinitions() method has moved to a different service, entity_field.manager.

With all of this said, you can use the same method to get taxonomy term fields as any other bundle. As documented in this related answer:

$entity_type_id = 'taxonomy_term';
$bundle = 'article_tags';

// Load the field definitions for any bundle.
$my_bundle_fields = \Drupal::service('entity_field.manager')
  ->getFieldDefinitions($entity_type_id, $bundle);
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top