[英]Drupal - Get custom taxonomy fields
I am trying to get a custom field assigned to taxonomy.我正在尝试将自定义字段分配给分类法。 I have tried this:
我试过这个:
$vid = 'zeme';
$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
$terms is now storing all the terms from the vocabulary called 'zeme'. $terms 现在存储名为“zeme”的词汇表中的所有术语。 The problem is when I print this variable, it doesnt show the custom field that I need to get.
问题是当我打印这个变量时,它没有显示我需要获取的自定义字段。 Any idea how can I get this custom field?
知道如何获得此自定义字段吗? My code looks like this:
我的代码如下所示:
$vid = 'zeme';
$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($terms as $term) {
$term_data[] = array(
'id' => $term->tid,
'name' => $term->name
);
}
Here is the loadTree function
official documentation : TermStorage::loadTree这是
loadTree function
官方文档: TermStorage::loadTree
When you use the loadTree
function, it will only get you the minimal datas to save execution time.当您使用
loadTree
函数时,它只会为您提供最少的数据以节省执行时间。 You can see there is a $load_entities
parameter set to false
by default.您可以看到
$load_entities
参数默认设置为false
。
bool $load_entities : If TRUE, a full entity load will occur on the term objects.
bool $load_entities :如果为 TRUE,则术语对象上将发生完整的实体加载。 Otherwise they are partial objects queried directly from the {taxonomy_term_data} table to save execution time and memory consumption when listing large numbers of terms.
否则,它们是直接从 {taxonomy_term_data} 表中查询的部分对象,以在列出大量术语时节省执行时间和内存消耗。 Defaults to FALSE.
默认为 FALSE。
So if you want to get all the datas of each of your taxonomy terms, you have to set $load_entities
to true
.因此,如果您想获取每个分类术语的所有数据,则必须将
$load_entities
设置为true
。
$vid = 'zeme';
$terms =\Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($vid, 0, null, true);
Found this way from this post Get custom fields assigned to taxonomy :从这篇文章中找到这种方式获取分配给分类法的自定义字段:
$contact_countries = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('contact_country');
$terms = array();
foreach($contact_countries as $contact_countrie) {
$terms[] = array(
'contact_country' => $contact_countrie->name,
'contact_phone' => \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($contact_countrie->tid)->get('field_phone')->getValue()[0]['value'],
'contact_flag' => \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($contact_countrie->tid)->get('field_category_flag')->entity->uri->value,
);
}
Very usefull!非常好用!
public function getTaxonomyBuild(){ $terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('faq_sec');公共 function getTaxonomyBuild(){ $terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('faq_sec');
foreach($terms as $term) {
$term_data[] = array(
'name' => $term->name,
'img' => file_create_url(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term->tid)->get('field_sec_img')->entity->uri->value),
);
}
return $term_data;
}
good solution好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.