We are doing the following query below to fetch all the taxonomy term values in the term_category vocabulary term. When we do the following query below, we get NULL value returned.

Content Type:

  • The Article content type has a field_category that is set to this new term_category taxonomy term.
  • Currently no Article nodes have any value populated for the field_category field as it is optional.

Taxonomy Term:

  • machine name = term_category
  • Label = Category
  • no fields other than title and description (which are out of the box)
  • has 15 values already added to the term (such as Cloud, IT, Security)


  • Using the tid from the $term_categories result set, iterate through each one to get the name of the Taxonomy term.

How come I cannot get the name of the taxonomy term value from term id?

$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. 

  // Goal. 
  ksm($category_term->getName()); // Returns Cloud

Output of ksm($categor_term) -> content

term content


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.

| improve this answer | |
  • I get Call to undefined method Drupal\taxonomy\Entity\Term::loadByProperties(). I see what you are saying, but in checking Term.php, I could not find loadbyProperties() method. – usernameabc Aug 20 '18 at 18:47
  • Sorry, mixed it up with something else. New code should work – Clive Aug 20 '18 at 18:48
  • you are loading the terms outside of the loop and it is able to get the correct entity, I am good to go! This made the method to get all the term values much more cleaner and simplified. For reference interface EntityTypeManagerInterface – usernameabc Aug 20 '18 at 19:01

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.

| improve this answer | |
  • 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 – usernameabc Aug 20 '18 at 18:40
  • Looking at it holistically rather than just switching tables, I ended up following Clive's suggestion – usernameabc Aug 20 '18 at 18:56
  • 1
    That was going to be my second suggestion, after you resolved the first issue so you saw why. – Kevin Aug 20 '18 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 Oct 2 '19 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

| improve this answer | |
  • 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 – usernameabc Aug 20 '18 at 18:39
  • Try the second Way. – berramou Aug 20 '18 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? – usernameabc Aug 20 '18 at 18:50

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.