Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

drupal-8

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?

like image 319
William A Hopkins Avatar asked Jan 20 '16 21:01

William A Hopkins


People also ask

How do I find my taxonomy ID in Drupal 8?

In Drupal 8 a taxonomy term can be loaded in the following way. $term = \Drupal\taxonomy\Entity\Term::load($termId); Where $termId is taxonomy term ID. Now to get name of taxonomy term, use getName() function.

What is taxonomy terms in Drupal?

Taxonomy, a powerful core module, allows you to connect, relate and classify your website's content. In Drupal, these terms are gathered within "vocabularies". The Taxonomy module allows you to create, manage and apply those vocabularies. Drupal 7 and 8 has the ability to add taxonomy fields to vocabularies and terms.


1 Answers

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.

$parent_tid = $parent->id()
like image 156
valdeci Avatar answered Jan 01 '23 15:01

valdeci