0

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?

1

3 Answers 3

2

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() ->getStorage('taxonomy_term') ->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.

0
1

The Term object has a method for getName and label.

https://api.drupal.org/api/drupal/core%21modules%21taxonomy%21src%21Entity%21Term.php/function/Term%3A%3AgetName/8.6.x

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.

4
  • 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. 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(). Commented Oct 2, 2019 at 20:05
1

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 ); } ksm($terms);  
3
  • 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. 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.