How do I get the parent tid of a taxonomy term in Drupal 8

William A Hopkins picture William A Hopkins · Jan 20, 2016 · Viewed 12.7k times · Source

I used the following to get the parent of a taxonomy term in drupal 8:

$parent = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($termId);

$parent = reset($parent);

Now that I have the parent how do I get the parent tid from that?

Answer

valdeci picture valdeci · Apr 12, 2017

Now that you have the term parent with the code:

$parent = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($termId);

$parent = reset($parent);

You can simply use the $parent->id() method to get your parent tid.