Drupal - 获取自定义分类字段

Ada*_*ulc 2 arrays drupal-taxonomy drupal-8

我正在尝试获取分配给分类法的自定义字段。我试过这个:

$vid = 'zeme';
$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
Run Code Online (Sandbox Code Playgroud)

$terms 现在存储名为“zeme”的词汇表中的所有术语。问题是当我打印这个变量时,它没有显示我需要获取的自定义字段。知道如何获得此自定义字段吗?我的代码如下所示:

 $vid = 'zeme';
  $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid); 
  foreach ($terms as $term) {
    $term_data[] = array(
      'id' => $term->tid,
      'name' => $term->name
    );
  }
Run Code Online (Sandbox Code Playgroud)

crt*_*tlf 5

这是loadTree function官方文档: TermStorage::loadTree

当您使用该loadTree功能时,它只会为您提供最少的数据以节省执行时间。您可以看到默认$load_entities设置为一个参数false

bool $load_entities:如果为 TRUE,则术语对象上将发生完整的实体加载。否则,它们是直接从 {taxonomy_term_data} 表中查询的部分对象,以在列出大量术语时节省执行时间和内存消耗。默认为 FALSE。

因此,如果您想获取每个分类术语的所有数据,则必须设置$load_entitiestrue.

$vid = 'zeme';
$terms =\Drupal::entityTypeManager()
  ->getStorage('taxonomy_term')
  ->loadTree($vid, 0, null, true);
Run Code Online (Sandbox Code Playgroud)