在Drupal 8表单字段中添加实体引用?

7
用户如何使用自动完成字段将产品添加到简单插件配置中?
我尝试使用 Config Entity,但它看起来与 Form API 相同(并且我无法在那里使用实体字段)。
2个回答

14

使用Drupal 8的表单API和entity_autocomplete类型,我可以实现这一点。

$form['stories'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#title' => $this->t('Stories'),
    '#description' => $this->t('Select some stories.'),
    '#default_value' => $default_entities,
    '#tags' => TRUE,
    '#selection_settings' => array(
        'target_bundles' => array('page', 'article'),
    ),
    '#weight' => '0',
];

这个可行!谢谢。我完成了添加自定义ContentEntityType的工作。虽然更复杂,但也更灵活。 - Przemek Leczycki
我向你竖起了拳头。 - camslice

-1

使用webform - 这里有一个实体引用字段。 这对你的目的来说不可用...但你可以为了好奇查看源代码。


这个问题是在2016年8月10日提出的,而不是在2010年。 - apaderno
true - 而 webform 或 eform 没有意义.. 但如果有人想通过 UI 创建表单,这是一个不错的选择。 - rémy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接