-1
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('category');
var_dump(tree);

the above snippet gives the following output:

array (size=4)
  0 => 
    object(stdClass)[389]
      public 'tid' => string '5' (length=1)
      public 'vid' => string 'category' (length=8)
      public 'langcode' => string 'en' (length=2)
      public 'name' => string 'General' (length=7)
      public 'description__value' => null
      public 'description__format' => null
      public 'weight' => string '0' (length=1)
      public 'changed' => string '1501133997' (length=10)
      public 'default_langcode' => string '1' (length=1)
      public 'depth' => int 0
      public 'parents' => 
        array (size=1)
          0 => string '0' (length=1)
  1 => 
    object(stdClass)[393]
      public 'tid' => string '6' (length=1)
      public 'vid' => string 'category' (length=8)
      public 'langcode' => string 'en' (length=2)
      public 'name' => string 'OBC' (length=3)
      public 'description__value' => null
      public 'description__format' => null
      public 'weight' => string '0' (length=1)
      public 'changed' => string '1501134006' (length=10)
      public 'default_langcode' => string '1' (length=1)
      public 'depth' => int 0
      public 'parents' => 
        array (size=1)
          0 => string '0' (length=1)
 .....

My question is how can i get the name i.e.,general,obc etc (i mean taxonomy term name) from the above snippet...i am new to drupal..plz help..

1 Answer 1

0

You can try this :

  $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('category');

  foreach ($tree as $key => $term) {
    $term_name = $term->name;
    var_dump($term_name);
  }

View this topic.

3
  • this gives me only one last term of vocabulary whereas i want all the terms of vocabulary..plz help...
    – user74783
    Jul 27, 2017 at 10:53
  • $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('category'); $categories=array(); $categories_arr = array(); for($i=0;$i<sizeof($tree);$i++){ // array_push($categories, $term->getName(($tree[$i]->tid))); $pracice_name=taxonomy_term_load($tree[$i]->tid)->get('name')->value; array_push($categories_arr,$pracice_name); }
    – user74783
    Jul 27, 2017 at 11:57
  • this solved my problem.
    – user74783
    Jul 27, 2017 at 11:57

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.