Drupal 11: Load Taxonomy Terms By Vocabulary

Using the entity query method on the Drupal object.

use Drupal\taxonomy\Entity\Term; 

$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', 'tags');
$query->accessCheck(FALSE);
$tids = $query->execute();

$terms = Term::loadMultiple($tids);

This is basically the same as using the entity query method via the storage system.

$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

$query = $storage->getQuery();
$query->condition('vid', 'tags');
$query->accessCheck(FALSE);
      
$tids = $query->execute();

$terms = $storage->loadMultiple($tids);

You can also use the storage system loadByProperties() method.

$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$terms = $storage->loadByProperties(['vid' => 'tags']);

The storage system for taxonomy terms also has a loadTree() method, which will load the hierarchical structure of a terms if the $load_entities property is set to true.

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(vid: 'tags', load_entities: TRUE);

Notice that all of these methods stem from the entity type manager service (entity_type.manager).

Add new comment

The content of this field is kept private and will not be shown publicly.