简体   繁体   English

从目标 ID 获取分类术语名称 - D9

[英]Getting Taxonomy term name from target ID - D9

Getting Taxonomy term name from Taxonomy target ID:从分类目标 ID 获取分类术语名称:

I have a taxonomy term that accepts multiple values.我有一个接受多个值的分类术语。 It's rendered as a multiselectfield.它呈现为多选字段。 I am trying to read the target ID of the field and figure the term name out of it using the below code in a preprocess function:我正在尝试读取字段的目标 ID,并在预处理函数中使用以下代码从中找出术语名称:

 $granttype = $user_entity->field_user_grant_type->getValue();
  foreach($granttype as $gt)
  {
    $granttype_name = \Drupal\taxonomy\Entity\Term::load($gt)->label();
  }
  dd($granttype_name);
  
  $variables['grant_type'] = $granttype_name;
  

dd($granttype) shows the below output: dd($granttype) 显示以下输出:

在此处输入图像描述

However, the foreach loop to figure out the term name is not working correctly.但是,用于确定术语名称的 foreach 循环无法正常工作。

dd($granttype_name) results as: dd($granttype_name) 结果为:

 The website encountered an unexpected error. Please try again later.
TypeError: Illegal offset type in Drupal\Core\Entity\EntityStorageBase->load() (line 297 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

I am looping through the target ID and trying to get the term name.我正在遍历目标 ID 并尝试获取术语名称。 But it's not working.但它不起作用。 Any help pls?有什么帮助吗?


UPDATE: I tried the below line of code:更新:我尝试了以下代码行:

 $term = term::load($gt);
    $name = $term->getName();

still no luck :( same error仍然没有运气:(同样的错误

Here is an example how to do this:这是一个如何执行此操作的示例:

  $grant_type = $user_entity->field_user_grant_type->entity;
  if ($grant_type instanceof \Drupal\taxonomy\TermInterface) {
    var_dump($grant_type->label());
  }

If you have multiple referenced terms, use:如果您有多个引用的术语,请使用:

  $grant_types = $user_entity->field_user_grant_types->referencedEntities();
  foreach ($grant_types as $grant_type) {
    var_dump($grant_type->label());
  }

Explanation:解释:

  1. The generic way to get entity title is the Entity::label method $term->label() ;获取实体标题的通用方法是Entity::label方法$term->label()
  2. There is a helpful method Entity::referencedEntities to get relations.有一个有用的方法Entity::referencedEntities来获取关系。

First, you need to include首先,您需要包括

use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;

Second, retrieve the value(s) stored in your field (field_user_grant_type) and store them in an array:其次,检索存储在您的字段 (field_user_grant_type) 中的值并将它们存储在一个数组中:

$myArray = array();
$granttype = $user_entity->get('field_user_grant_type')->getValue();

$granttype will now contain an array of arrays. $granttype现在将包含一个数组数组。 Next, you need to gather the actual term IDs接下来,您需要收集实际的术语 ID

foreach($granttype as $type){
   $myArray[] = $type['target_id'];
}

Finally, loop through $myArray and fetch the term IDs stored there, and then use each ID to get its corresponding Term Name.最后,遍历 $myArray 并获取存储在那里的术语 ID,然后使用每个 ID 获取其对应的术语名称。 Here, I store them in a new array called grantTypeNames在这里,我将它们存储在一个名为grantTypeNames的新数组中

grantTypeNames = array();
foreach($myArray as $term_id){
   $grantTypeNames[] = Term::load($term_id)->get('name')->value;
}

The array $grantTypeNames will now contain the term names you want.数组$grantTypeNames现在将包含您想要的术语名称。 I hope that helps.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM