We are using the following code to fetch all the taxonomy term values in the term_category vocabulary.

$db = \Drupal\Core\Database\Database::getConnection();
$term_categories = $db->query('select tid, vid from taxonomy_term_data where vid ='\'term_category\'');

foreach($term_categories as $category) {
  $category_term = (\Drupal\taxonomy\Entity\Term::load($category->tid));
  ksm($category_term->label()); // returns NULL.
  ksm($category_term->getName()); // returns NULL. 

When we run it, we get NULL as label.

The Article content type has a field_category field that is an entity reference for the term_category vocabulary. Currently, no Article node has any value populated for that field, as it is optional.

Why I cannot get the taxonomy term name from its ID?


3 Answers 3


Currently your problem is that you're trying to call entity class methods on a stdClass object - directly querying the database means you're not using the API any more, the results you get aren't entities.

Unless there's a particular reason to do so, though, you shouldn't have to do it that way.

For example, the same query can be executed like this through the API:

$terms = \Drupal::entityTypeManager()
  ->loadByProperties(['vid' => 'term_category']);

foreach ($terms as $term) {
  $name = $term->getName();

It's quicker (loading in one go, not single loads in a loop), fewer lines of code, and when you bring caching into it, probably more performant.


The Term object has a method for getName and label.


Therefore, if this is a real term entity, it will have a name accessible via getName.

You should debug $category in the loop to see you are accessing the right property.

  • I was indeed accessing the wrong property on the wrong entity. I was able to get it to work when switching tables from taxonomy_term_data to taxonomy_term_field_data Commented Aug 20, 2018 at 18:40
  • Looking at it holistically rather than just switching tables, I ended up following Clive's suggestion Commented Aug 20, 2018 at 18:56
  • 1
    That was going to be my second suggestion, after you resolved the first issue so you saw why.
    – Kevin
    Commented Aug 20, 2018 at 18:59
  • getName() won't be around too much longer. See Deprecate Node::getTitle() & other entity-type-specific calls in favour of Entity::label().
    – colan
    Commented Oct 2, 2019 at 20:05

Try This:

$term_category = 'term_category';
$term_data = [];
  $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($term_category);
  foreach ($terms as $term) {
    $term_data[] = array(
      'title' => $term->name,
      'description' => $term->description

  • when I ran this, the foreach loop ran 77 times where only 15 had values and all others returned NULL. However, i found my issue to be that i needed to switch to taxonomy_term_field_data Commented Aug 20, 2018 at 18:39
  • Try the second Way.
    – berramou
    Commented Aug 20, 2018 at 18:47
  • 1
    the second way did work, can you update your answer to reflect this so it is easier for others if they come across this? Commented Aug 20, 2018 at 18:50

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.