2016-05-06 43 views
0

我想在自定义模块中添加Drupal 8内容类型的额外字段,并且我没有得到任何挂钩。模块挂钩在drupal 8内容类型中添加额外字段

下面是我使用挂钩但这是没有帮助我的结果,我想:

function nodeclass_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { 
// Add a property only to nodes of the 'article' bundle. 
    if ($entity_type->id() == 'node' && $bundle == 'article') { 
    $fields = array(); 
    $fields['mymodule_text_more'] = BaseFieldDefinition::create('string') 
     ->setLabel(t('More text')) 
     ->setComputed(TRUE) 
     ->setClass('\Drupal\mymodule\EntityComputedMoreText'); 
    return $fields; 
    } 
} 
+0

没有人在这里回答这个问题? –

+0

这可能还不够,您还需要hook_entity_field_storage_info。 – golddragon007

+0

你也可以看这个:https://drupal.stackexchange.com/questions/220175/add-field-to-bundle-through-hook-entity-bundle-field-info/220177#220177和文件说你需要hook_entity_field_storage_info或者您需要覆盖现有的字段库。你不做这两件事中的任何一件。 – golddragon007

回答

0

里面你的模块文件夹,创建以下文件夹 模块/ my_module /配置/安装/

然后在模块文件夹内创建下面的文件。

my_module /配置/安装/ field.field.node.article.field_text_more.yml

langcode: en 
status: true 
dependencies: 
    config: 
    - field.storage.node.field_text_more 
    - node.type.article 
id: node.article.field_text_more 
field_name: field_text_more 
entity_type: node 
bundle: article 
label: 'More text' 
description: '' 
required: false 
translatable: false 
default_value: { } 
default_value_callback: '' 
settings: { } 
field_type: string 

my_module /配置/安装/ field.storage.node.field_text_more.yml

langcode: en 
status: true 
dependencies: 
    module: 
    - node 
id: node.field_text_more 
field_name: field_text_more 
entity_type: node 
type: string 
settings: 
    max_length: 255 
    is_ascii: false 
    case_sensitive: false 
module: core 
locked: false 
cardinality: 1 
translatable: true 
indexes: { } 
persist_with_no_fields: false 
custom_storage: false 

然后重新安装你的模块,它会将新的字段“更多文本”添加到“文章”内容类型中。

注意:如果您希望将字段添加到任何其他内容类型,请更改文件名并更新field.field.node.article.field_text_more.yml中的行“bundle:article”。

+0

如果您的模块含有内容,这不是一个好主意,因为当您禁用该模块时,所有相关内容都将被删除 – golddragon007

相关问题