要按要求工作,您需要 3 样东西:
- 在您的自定义实体中实现 entity_reference 字段。
- 为您的字段添加一个 getter 方法。
- 在您的自定义 ListBuilder -> buildRow() 中检索您的字段。
查看有关 FieldTypes, FieldWidgets and FieldFormatters 的 Drupal 8 文档。
实现 entity_reference 字段
您的实体中的字段 foo 应使用 entity_reference 字段类型生成。
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 ...
}
然后,您应该将 3 个 vocabulary_id 替换为您想要链接的词汇。
添加一个吸气剂
与您的 baseFieldDefinitions 在同一类中。
// Some code ...
public function getFoo() {
return $this->get('foo')->value;
}
// Some code ...
检索字段
在您的 ListBuilder 类中。
public function buildRow(EntityInterface $entity) {
// Some code ...
$row['foo'] = $entity->getFoo();
// Some code ...
}
希望对你有帮助!