0

I want to load parent term name by tid .I am using this

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($tid);
kint($term);

This return a object which is my parent term but I am not able to access term name by this object. Please check attached screenshot of kint($term). enter image description here

1

You should get the name with the getName method. In your case :

$term[1]->getName();
$term->getName();

If not, you can load the term and after get the name:

$term = Term::load($term[1]->id());
$term->getName();
  • If i want get vid then ? – Pankaj Yogi May 11 '17 at 13:06
  • $term->getVocabularyId() – jBlobsmith May 11 '17 at 19:15
1
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($tid);
$term_name = $term[key($term)]->getName();

jBlobsmith answer is not correct because he is using static term id ($term[1]) if parent's term id is 3 than it will not work.You have to change this key manualy in variable ($term[3]).

  • If i want get vid then ? – Pankaj Yogi May 11 '17 at 13:12
  • That's why I specified "In your case"... – jBlobsmith May 11 '17 at 19:19
  • $term[key($term)]->getVocabularyId(); – Jeetendra Kumar May 12 '17 at 6:05

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.