【问题标题】:Drupal 8 - custom field plugin & specific taxonomy vocabularyDrupal 8 - 自定义字段插件和特定分类词汇
【发布时间】:2018-04-20 18:25:57
【问题描述】:

我正在 Drupal 8 中构建一个自定义复合字段。我快到了,但我还缺少最后一点:当我将此新字段类型添加到内容类型时,分类自动完成字段会从每个分类中提取网站上的词汇。

我正在尝试确定如何仅从特定的“plant_parts”词汇中提取。目前,在我的小部件代码中,我有这个:

$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, 
  );

【问题讨论】:

  • 实际上..取得了进展,插入:'#selection_settings' => array('target_bundles' => array('taxonomy_term', 'plant_part'), ),

标签: plugins drupal taxonomy fieldtype


【解决方案1】:

您可以使用 STATECONFIG API 来保存默认值并在#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'
    ],
  ],
];

【讨论】:

  • 只是添加#selection_settings' => ['target_bundles' => 'plant_part'],似乎有帮助,除了两个问题。一,当它呈现在页面上时,它呈现的是分类 id,而不是术语。但是,当我尝试编辑它时也会出错。关于“实体自动完成”的一些长错误
  • 你实际在哪里渲染这个?我认为错误应该与#default_value尝试将实体作为#default_value而不是id相关联。
  • 终于回到了这个任务……而且没有任何运气。我在我的小部件代码中使用了上面的脚本,但我仍然没有看到以前选择的术语出现在该字段中。一切正常 - 它只是没有显示该字段中的值。
猜你喜欢
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
相关资源
最近更新 更多