[英]How do I get the parent tid of a taxonomy term in Drupal 8
I used the following to get the parent of a taxonomy term in drupal 8:我使用以下内容来获取 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?现在我有了父母,我如何从中获得父母的信息?
Now that you have the term parent with the code:现在您有了带有代码的术语 parent:
$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->id()
方法来获取您的父tid 。
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term_id);
$parent_term_id = $term->parent->target_id;
It will provide the parent id of the term if exist.如果存在,它将提供术语的父 ID。
You can pull the tree for the vocabulary and sift through that.您可以拉出词汇树并对其进行筛选。
// assuming $termId is the child tid..
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('VOCABULARY_NAME', 0);
for ($tree as $term) {
if (in_array($termId, $term->parents)) {
$parent_term = $term;
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.