0

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)

Goal:

  • 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

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.

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

  • 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
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);

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