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?
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With