Plugins Drupal 8-自定义字段插件&;特定分类词汇
我正在Drupal8中构建一个自定义复合字段。我就快到了,但我缺少的最后一点是:当我将这个新字段类型添加到内容类型时,taxonomy autocomplete字段从站点上的每个分类词汇表中提取 我试图确定如何仅从特定的“plant_parts”词汇中提取。目前,在我的小部件代码中,我有以下内容:Plugins Drupal 8-自定义字段插件&;特定分类词汇,plugins,drupal,taxonomy,fieldtype,Plugins,Drupal,Taxonomy,Fieldtype,我正在Drupal8中构建一个自定义复合字段。我就快到了,但我缺少的最后一点是:当我将这个新字段类型添加到内容类型时,taxonomy autocomplete字段从站点上的每个分类词汇表中提取 我试图确定如何仅从特定的“plant_parts”词汇中提取。目前,在我的小部件代码中,我有以下内容: $element['plant_component_measured'] = array( '#type' => 'entity_autocomplete', '#tar
$element['plant_component_measured'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => t('Plant part'),
'#prefix' => '<table><tr><td>',
'#suffix' => ' ',
'#default_value' => isset($items[$delta]->plant_component_measured) ?
$items[$delta]->plant_component_measured : NULL,
);
$element['plant\u component\u measured']=数组(
“#类型”=>“实体自动完成”,
“#目标_类型”=>“分类法_术语”,
“#标题”=>t(“工厂部分”),
“#前缀”=>”,
“#后缀”=>”,
“#默认值”=>isset($items[$delta]->工厂组件测量值)?
$items[$delta]>测量的设备组件:空,
);
您可以使用STATE或CONFIGAPI保存默认值,并在#selection_settings['target_bundles']=['plant_part']
中指定分类类型,如下所示
use Drupal\taxonomy\Entity\Term;
:
:
if (empty(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured')))
\Drupal::state()->set('YOUR_MODULE_NAME.plant_component_measured', '');
$entity = Term::load(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured'));
$element['plant_component_measured'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => $this->t('Plant part'),
'#description' => $this->t('Plant part'),
'#prefix' => '<table><tr><td>',
'#suffix' => ' ',
'#default_value' => $entity,
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => [
'plant_part'
],
],
];
使用Drupal\taxonomy\Entity\Term;
:
:
if(空(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured'))
\Drupal::state()->set('YOUR_MODULE_NAME.plant_component_measured','');
$entity=Term::load(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured');
$element['工厂组件测量']=[
“#类型”=>“实体自动完成”,
“#目标_类型”=>“分类法_术语”,
“#title”=>$this->t(“工厂零件”),
“#description”=>$this->t(“工厂零件”),
“#前缀”=>”,
“#后缀”=>”,
“#默认_值”=>$entity,
“#选择处理程序”=>“默认值”,
“#选择_设置”=>[
“目标捆绑包”=>[
“植物部分”
],
],
];
实际上。。取得了进展,插入:“#选择设置”=>array('target_bundles'=>array('taxonomy_term','plant_part')),只是添加了#选择设置”=>['target_bundles'=>'plant_part],似乎有帮助,除了两个问题。第一,当它在页面上呈现时,它呈现的是分类id,而不是术语。但是,当我试图编辑它时,也会出错。关于“实体自动完成”的一些长时间错误,您在哪里实际渲染它?我认为错误应该与#default_值
关联,尝试将实体作为#default_值
而不是id传递。最后返回到该任务。。。而且一点运气都没有。我在我的小部件代码中使用了上面的脚本,但仍然没有看到先前选择的术语出现在字段中。一切都在运转——它只是没有显示字段中的值。