如何在Drupal 8

时间:2016-01-20 21:31:30

标签: drupal-8

我使用以下内容在drupal 8中获取分类术语的父级:

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

$parent = reset($parent);

现在我有父母如何从中获取父级?

3 个答案:

答案 0 :(得分:4)

现在您的术语为parent,代码为:

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

$parent = reset($parent);

您只需使用$parent->id()方法即可获取您的父 tid

答案 1 :(得分:0)

你可以拉树来获取词汇并筛选出来。

// 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;
  }
}

答案 2 :(得分:0)

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term_id);
$parent_term_id = $term->$parent->target_id;