在drupal 8自定义实体的buildrow函数中检索分类术语 [英] Retrieve a taxonomy term in the buildrow function of a drupal 8 custom entity
问题描述
我建立了一个运作良好的自定义实体。我的一个字段是分类法,但我无法在显示记录的 buildRow(EntityInterface $ entity)
函数中检索该术语的名称。
对于一个简单的字符串字段,我这样做: $ row ['foo'] = $ entity-> foo-> value;
如何做为实体引用的分类术语: $ row ['bar'] = $ entity-&B; BAR_TERM_NAME;
谢谢您的帮助。
按要求工作,您需要三件事:
- 在自定义实体中实现 entity_reference 字段。
- 为您的字段添加一个getter方法。
- 在自定义的 ListBuilder -> buildRow()中检索您的字段strong>。
查看有关字段类型,字段小部件和FieldFormatters 。
实现 entity_reference 字段
您实体中的字段 foo 应该使用 entity_reference 字段类型生成。
公共静态函数baseFieldDefinitions(EntityTypeInterface $ entity_type){
//一些代码...
$ fields ['foo'] = BaseFieldDefinition :: create('entity_reference')
-> setLabel($ this-&t; t('Foo field'))
-> setDescription($ this-&t; t('The Foo字段))
-> setSetting('target_type','taxonomy_term')
-> setSetting('handler','default')
-> setSetting('handler_settings) ',['target_bundles'=> [’vocabulary_id’=> 'vocabulary_id']])
-> setDisplayOptions('view',[
'label'=>'hidden',
'type'=>'vocabulary_id',
'weight'=> 0,
])
-> setDisplayOptions('form',[
'type'=>'options_select',
'weight '=> 40,
])
-> setDisplayConfigurable('form',TRUE)
-> setDisplayConfigurable('view',TRUE);
//一些代码...
}
您然后应将3 vocabulary_id 替换为您想要链接的词汇。
添加吸气剂
与您的 baseFieldDefinitions 处于同一类。
//一些代码...
公共函数getFoo(){
返回$ this-> get('foo')-> value;
}
//一些代码...
检索字段
在您的 ListBuilder 类中。
public函数buildRow(EntityInterface $ entity){
//一些代码...
$ row ['foo'] = $ entity-> getFoo();
//一些代码...
}
希望它对您有帮助!
I have built a custom entity that works well. One of my fields is a taxonomy but I can not retrieve the name of the term in the buildRow(EntityInterface $entity)
function which displays my records.
For a simple string field I do: $row['foo'] = $entity->foo->value;
How to do a taxonomy term that is an entity_reference: $row['bar'] = $entity->BAR_TERM_NAME;
Thank you for your help.
To work as requested you need 3 things:
- Implements an entity_reference field in your custom Entity.
- Add a getter methode for you field.
- Retrieve your field in your custom ListBuilder -> buildRow().
Check the Drupal 8 documentation about FieldTypes, FieldWidgets and FieldFormatters.
Implements an entity_reference field
Your field foo in your Entity should be generated using the entity_reference field type.
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Some code ...
$fields['foo'] = BaseFieldDefinition::create('entity_reference')
->setLabel($this->t('Foo field'))
->setDescription($this->t('The Foo field.'))
->setSetting('target_type', 'taxonomy_term')
->setSetting('handler', 'default')
->setSetting('handler_settings', ['target_bundles' => ['vocabulary_id' => 'vocabulary_id']])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'vocabulary_id',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 40,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
// Some code ...
}
You should then replace the 3 vocabulary_id by the vocabulary that you wanna links.
Add a getter
In the same Class as your baseFieldDefinitions.
// Some code ...
public function getFoo() {
return $this->get('foo')->value;
}
// Some code ...
Retrieve the field
In your ListBuilder Class.
public function buildRow(EntityInterface $entity) {
// Some code ...
$row['foo'] = $entity->getFoo();
// Some code ...
}
Hopes it will help you !
这篇关于在drupal 8自定义实体的buildrow函数中检索分类术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!